Tuesday, August 25, 2015

Execute java Code in Ubuntu

One thing about Java is cross platform where it able to run at any machine with Java environment installed wile for VB and C# that mainly run in window environment. So now I here to record how to execute java code in Ubuntu.

Open up your terminal in Ubuntu.

Make sure your machine is install with Java. To check the version of Java you may use below command.

java -version

If there is no respond of Java version from the show version command, you may need to install Java.
Below is the command install Java version 1.8. Execute below command line by line, It take few minutes to download the Java and it based on your network connection.

sudo add-apt-repository ppa:webupd8team/java -y
sudo apt-get update
sudo apt-get install oracle-java8-installer
Once finish install, verify it by using java -version. So you may get to know the version of Java installed in the machine.

Create a java code in any directory that is accessible with read write access. Below is Hello world sample code for testing purpose and save the file name as myFileName.java while the Class name is TestJavaProgram.

//First Java Program class TestJavaProgram
{ 
    public static void main (String[] args)  
   {  
       System.out.println ("Hello, world."); 
   } 
}  
So after save the java file in a directory, using terminal dir command navigate to the particular folder/directory. After in the level of the directory, using below code to compile the java.

javac myFileName.java
After the compilation, you need to run the java code by using below command and make sure the class name is correct.

java TestJavaProgram
Alternative way some people may make the java code as a jar file. You may execute the jar file by using below command.

java -jar yourjarfilename.jar


No comments:

Post a Comment