[ New messages · Members · Forum rules · Search · RSS ]
  • Page 1 of 1
  • 1
Forum moderator: shanky  
Forum » IT Forum » Technical questionare on Computer languages » Compile and run a simple java program (How to compile and run a simplest java program)
Compile and run a simple java program
dddDate: Sunday, 2014-09-21, 11:50 AM | Message # 1
Private
Group: Users
Messages: 2
Awards: 0
Reputation: 0
Status: Offline
Hi there
I want to compile and run a java program from command prompt and/or from an IDE.
Can you help me in this?

The program should simply print a string.

Thanks
Dipti Vyas
 
shankyDate: Sunday, 2014-09-21, 12:15 PM | Message # 2
Lieutenant
Group: Administrators
Messages: 46
Awards: 0
Reputation: 104
Status: Offline
Hi ddd
 
If you are executing a program from command prompt, you have to follow below steps:
  • Navigate to bin folder of you java program
  • cd C:\Program Files\Java\jdk(version)\bin>
  • Make sure that java comiler executable is there. i.e. javac.exe because this is the program which will compile and generate the bytecode for your java program.
  • Now create a simplest java program with name "Apples.java" in the same folder where "javac.exe" is . Put following code into the file:

public class Apples{
public static void main(String[] args) {
  System.

out.println("Shankar");
}

}

  • Make sure that the java file name and the class name should be the same(letter and case)
  • Now compile the java program.

javac Apples.java   <Press Enter>
  • If the program has no compilation errors, it will be compiled and byte code (.class file) will be generated or else fix the compilation errors
  • For just printing a string, no imports are required.
  • Now, execute the program.

java Apples <press enter>
  • You should get the output as:

Shankar


s.k.bhagat
 
shankyDate: Sunday, 2014-09-21, 12:35 PM | Message # 3
Lieutenant
Group: Administrators
Messages: 46
Awards: 0
Reputation: 104
Status: Offline
If you set the path of your java compiler, you can compile and run your java program from anywhere.
For that, you just need to set the path.
 
set path=%path%;C:\Program Files\Java\jdk<your version>\bin
 
This will append the path of javac.exe to the path value.
Now suppose your java program is at C:\Dipti\Apples.java
from there also, you can perform
javac Apples.java
and
java Apples


s.k.bhagat
 
Forum » IT Forum » Technical questionare on Computer languages » Compile and run a simple java program (How to compile and run a simplest java program)
  • Page 1 of 1
  • 1
Search: