Navigation Menu

Currently the navigation bar is not functioning. Use the Blog Archive or the Google Custom Search to find content.

Friday 31 May 2013

Compiling and running Java program

To create a Java program type the code in a file and save it as ClassName.java (where ClassName is the name of the class in java file). If the ClassName doesn't match with the name of the class in ClassName.java file the program wont compile. The class you are compiling should have a main() method since this is where the program begins its execution. A program can have one main() method and any number of classes. You dont need to compile individual classes.

Steps to compile and run :

Method 1 :
Click on start menu
Click on Run (Alternatively you can press Win+R keys)
Type cmd in the run window and press Enter key
In order to compile Java program you need to set path
set path=[drive:]\Program Files\Java\jdkx.x.x_x\bin;
where x.x.x_x is the version
In the command prompt type the path to locate the class file
cd /d [drive:]\path
Then to compile the program type 
javac ClassName.java
where ClassName.java is the file that contains the main() method.
Then to run the program type
java ClassName
Note : Notice in the above we didnt mentioned the .java extension that is because the ClassName.java file doesn't run. Instead the ClassName.class file that is created after compilation is run.

Method 2:
Click on start menu
Click on Run (Alternatively you can press Win+R keys)
Type notepad in the run window and press Enter key
In order to compile Java program you need to set path. 
Type in notepad
set path=[drive:]\Program Files\Java\jdkx.x.x_x\bin;
where x.x.x_x is the version
Then to compile the program type in notepad
javac ClassName.java
where ClassName.java is the file that contains the main() method.
Then to run the program type in notepad
java ClassName
Save the file in the same directory where your .java file exists.
Name it as Filename.bat (where Filename is name of the file for 
example run.bat)
Double click on the .bat file to run.
Note : For programs that require an argument while running you have to specify the parameter/s in the file while saving (for example java ClassName 1 2 3)where 1 2 3 are three parameters that are separated using space.

For Netbeans click on the link below :
https://netbeans.org/kb/docs/java/quickstart.html

Note :If you feel something is incorrect or missing leave a comment below. Please report about broken links.