Jul 3, 2012

Compile and Run a Hello-world Java Program


Step 1


 


Open "Folder" and create a new directory called "myproject" under your home directory to keep all your works.




Step 2


 


Open "Text Editor" (gedit). Enter the following source code and save as "Hello.java" under the "~/myproject" directory created eariler.


 


    public class Hello {   // To save as "Hello.java" under "~/myproject"


       public static void main(String[] args) {


          System.out.println("Hello, world from Ubuntu!");


       }



    }










Step 3



To compile the Hello-world Java program, launch a Terminal and issue these commands:


 


// Change directory to where the source code resides


cd ~/myproject


     


// List the contents of current directory. Check for "Hello.java"


ls


    ...... Hello.java ....


     


// Compile "Hello.java" into "Hello.class"


javac Hello.java


     


// Check for "Hello.class"


ls


    ...... Hello.class ....




Step 4


 


Run the Hello-world Java program:


 


// Run "Hello.class"


java Hello



    Hello, world from Ubuntu!



Compile and Run a Hello-world Java Program






 

No comments:

Post a Comment