Saturday, July 23, 2011

How to convert JAR files to EXE files

Hello All :)


How are you all :) 


Well in this post i will be providing your softwares and steps to convert your JAR file to EXE file


This post will give you brief steps to convert you jar to exe file


Lets begin ;)


But before we begin let me show you the code and the jar file first. Below is the screenshot of the code and the jar file icon on the desktop




Code that creates a frame and adds 3 button in a Flow Layout style




This is the Frame.jar of the above code


Steps:
NOTE: My jar file name is Frame.jar 

1. Open notepad and type start javaw -jar <your jar file name>.jar and save it as <anything>.bat. Below is my Frame.bat file content


 2. Now download this file Click Here and unzip this file and open Bat_To_Exe_Converter.exe. Below frame will be displayed


Select the .bat file that you created and give the file name and path of your exe file 

It is optional to add the version etc. after completing this click on compile

3. Congrats your exe file is ready to use :) :) 


Thats it for this post


Take Care


Peace




For Any Query mail me: ravjot.singh.28@gmail.com

Compile and Execute Java Code with packages in CMD

Hello all ;) 


Today I am providing you all quick crash course on how to compile and execute java codes using CMD but the there is a small catch :P the java codes are stored in packages ;)


So, i have created 3 packages A,B,C 
A contains a.java class   [this is the entry point i.e. a.java contains public static void main(String ap[]) method]
B contains b.java class
C contains c.java class


Lets begin, below are the steps accompanied by screenshots to make it easy to understand and implement


Steps are as follows:


1. Create three folders at your desired location the folders are: A,B, and C
    P.S. : I have created these folders on my Desktop :)

    
2. Open your notepad and create a.java below is the screenshot of a.java

    
and save this a.java in A folder
    NOTE: import B.b links the b class stored in B package

3. Open Notepad and create b.java file, below is the screenshot of the code

    
and save this b.java in B folder
    NOTE: import C.c links the c class stored in C package

4. Open Notepad and create c.java file, below is the screenshot of the code

    
and save this c.java in C folder

5. Now comes compilation, We have to be careful in this step. We can not directly compile in any order.
    We have to follow the order, for example in this case where I have A,B, and C package i will compile that java file which is not dependent on any package i.e. which do not import any class which we have created. In this case c class should be compiled first and then b class should be compiled then a class.


If you do not follow this series then you will encounter errors during javac command :(
Below is the screen shot of the cmd during compiling and executing the code

    
NOTE: use / for packages 

6. Give a smile :) you have successfully compiled and executed java files using packages in CMD :) :)

    NOTE: this style can be used in Mac and Linux also :) so enjoy


Thats it for this post


Take Care


Peace


For Any Query mail me: ravjot.singh.28@gmail.com

Thursday, July 14, 2011

How to Set Path after Installing Java SDK [Windows]

Hello All 


This post is providing you all a short and crisp procedure to Set Path after installing Java SDK in Windows.


Steps:

1. Go to the location where you installed Java SDK, mostly you can find it in Program Files folder 
    i.e. C:\Program Files\Java

2. This Java folder contains 2 folders: jdk and  jre folders accompanied by their version numbers 

3. Go to JDK folder and inside JDK folder move to Bin folder

4. Copy the complete address of this location. 
    My address is : C:\Program Files\Java\jdk1.6.0_21\bin


5. Right click on My Computer/Computer and click on properties.

6. Select Advanced System Settings  on the left of the screen (For Vista and Win 7) 
    Or
    Select Advanced tab (for Win XP)


7. Click on Environment Variables at the bottom of the dialog box.


8. NOTE: Be careful now follow the steps carefully 
    Under the System Variables search for Path under Variable column

9. After Selecting the Path click on edit


10. In the Variable Value move to the end of the text written in it (By pressing End key or navigating using Right arrow key)


11. Type  ;(semicolon) at the end of the string and paste the address you copied
       Eg: ;C:\Program Files\Java\jdk1.6.0_21\bin

12. Add this after bin \;.; (Slash,semicolon,dot,semicolon)
      Eg: ;C:\Program Files\Java\jdk1.6.0_21\bin\;.;


13. Click Ok and Ok and Ok :)

14. Open CMD NOTE: If CMD was already open close that CMD and open new CMD and type javac and press enter.

15. If you get a list of argument list in CMD then you have successfully set the path, else try again



Congrats :) Mission Accomplished 


Take Care


Peace



For Any Query mail me: ravjot.singh.28@gmail.com

Thursday, July 7, 2011

JNI (Java Native Interface) HelloWorld Code[How To]

Hello all :)


Hope you all are having great time with your LOVE (JAVA :P )


Well we all have read about JNI i.e. Java Native Interface.


What is JNI?


Well JNI is a programming framework that allows Java to execute applications specific to a hardware or an Operating System Platform written in various languages: C,C++ or assembly


Now we all know what is JNI, but 
HOW TO IMPLEMENT IT?


I am providing u steps with screenshot to make it easy to understand and implement


But before you begin coding your 1st code using JNI we have to install 2 things


  •  SDK (includes JDK and JRE)
  •  Microsoft Visual C++




Steps To implement HelloWorld code using JNI


1. Create a normal java file but include a native method in your code. Sample code is given below


2. Now save this code HelloWorld.java and compile it using CMD/Terminal
javac HelloWorld.java



3. After successful (NOTE: javac HelloWorld.java executed without any error) we have to execute this command in CMD/Terminal itself

javah -jni HelloWorld



After this execution you will have 3 files -> HelloWorld.java, HelloWorld.class and HelloWorld.h



4. Now open HelloWorld.h file. The file will be as follows






5. Now this HelloWorld.h file is generated automatically. We will edit this code so that this HelloWorld.h can perform some operation.
To do this we have to define the method 
Java_HelloWorld_sayHello(JNIEnv *,jobject,jstring)

Here is the modified code


6. Now we require DLL file that can be embedded in our HelloWorld.java file
We use Microsost Visula C++ command-line tool. Before that rename HelloWorld.h to HelloWorld.c and then

type the below command to get the dll file

cl -c /I<your jdk path>\include /I<your jdk path>\include\win32 HelloWorld.c link /libpath=<your jdk path>\lib HelloWorld.obj /dll

Eg:
cl -c /Ic:\jdk1.1.6\include /Ic:\jdk1.1.6\include\win32 HelloWorld.c

link /libpath=c:\jdk1.1.6\lib HelloWorld.obj /dll
7. Now comes modification of the HelloWorld.java file. Modified code is given below
8. Congrats you completed your 1st JNI HelloWorld code
Now to execute your code
compile the HelloWorld.java code again and execute it using command java HelloWorld ( NOTE: These commands are executed in your terminal/CMD)
OUTPUT: Hello


Sorry currently i m not having Microsoft Visual C++ so cant show you the dll file and final execution
Congrats you just learned to create your own code using JNI :)
Take Care
Peace






For Any Query mail me: ravjot.singh.28@gmail.com