java on linux:Learn how to write,compile and run java programs in Linux.

Posted: May 8, 2015 in Tips and Tricks
Tags: , , , , ,

If you are a java programmer and you want to write or run  your java program on linux platform this article is for you.this article will help every java programmers who want to understand how to write and execute java programs on linux os.My motive behind this article is just to show you how to write,compile and run a java program in linux machine and for that i am using a simple java program.Here in this article i have just used a simple java program examples to show you how you write,save,compile and run this java program in Red hat Enterprise Linux.

java on linux

Step1:Install packages required to compile and run java programs in linux.

You can install java packages in so many ways:You can install it directly using rpm packages but if yum server is configured on your system i suggest you to install it using yum like i mentioned below.

 

#yum install java

Step2:Write  a java program:

Lets create your first java application,shivangi which will simply display a greeting “Hello shivangi”

Now to create this application you need:

1.To create a source file.

If you are a programmer i hope you understand that a source file is a file that contains the code and here the code will be written in java programming language.You can use any text editor like vi/vim,getedit,nano etc to create,edit or write your source code.

Here i am going to write a java program named shivangi.java using vim editor.You can use any other editor like nano too to write your java program or any programming language.

Note:our class name must match our file name.

Note:There are 3 types of comments used in java programming language.Altough comments are ignored by compiler but useful for programmers to understand the program.

root@localhost ~]#vi shivangi.java

/** * The shivangi class show an application that * simply prints "Hello shivangi" to standard output. */

 public class shivangi {

       public static void main(String[] args) {
        System.out.println("Hello, shivangi");//display the string
    }
}

 

Step3:Compile your java program using javac:

javac which is java programming language compiler takes your source file and compile or you can say translate its text in to instructions so that java virtual machine can understand it.

 

 [root@localhost  ~]#javac shivangi.java

 

 

Step4:Run your java Program.
“java” is a java application launcher tool, which uses the java virtual machine to run your application.

 

   [root@localhost  ~]# java shivangi
      Hello shivangi

 

Java

Note:Here we  will use the Java compiler javac to compile  Java programs and the Java interpreter java to run them.

If you want to know which version of java is installed on your RedHat machine.

 [root@localhost ~]# java -version
java version "1.6.0"
OpenJDK  Runtime Environment (build 1.6.0-b09)
OpenJDK Server VM (build 1.6.0-b09, mixed mode)

 

If you want to know some more about your java packages installed in your linux machine you can use below command.

 [root@localhost ~]# which java
/usr/bin/java

[root@localhost ~]# whereis java
java: /usr/bin/java /etc/java /usr/lib/java /usr/share/java /usr/share/man/man1/java.1.gz

 

SOME KEYWORDS WHICH MUST BE KEPT IN MIND WHILE WRITING JAVA PROGRAM.

 javac – JAVAC is the Java programming language compiler.

The  javac  tool  reads class and interface definitions, written in the
Java programming language, and compiles them into byte code class files.

 

 jdb – Java Application Debugger

jdb helps us to  find and fix bugs in Java programming language.

 

 javah – C Header and Stub File Generator

JAVAH  produces C header files and C source files from a Java class. These files provide the connective glue that allow your Java  and C code to interact.

 

jar – JAR Archive Tool

jar combines multiple files into a single JAR archive file.

Comments
  1. mayank says:

    Sir what will happen when i redirect:-
    /etc/passwd >/dev/sdb
    interviewer asked me this question and at that time i had not any idea about it..

    Like

  2. Anonymous says:

    how to apply the patches form redhat satellite server please help

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s