Blogger templates

How to compile and run java programs Manually

Let’s have a look at how we can compile and run java programs without the help of any IDEs (that’s DrJava) by just using a CLI (Command Line Interface, that’s cmd on Windows and terminal in Ubuntu).

First of all, an IDE is nothing but basically a text editing software with some additional tools like syntax highlighting (that’s the different color of text you get when writing codes in DrJava), Debugging, Code Completion, Auto Code Compilation and running and some more helpful tools in more advanced and powerful IDEs. Now we can basically write our source code in any text editor and then compile it manually and run it with JVM. Today we’re going to use NOTEPAD (or you can use Gedit or Vim if you’re using Linux).

Open up NotePad.
Write the following code in it!

public class test{
    public static void main(String[] args){
        System.out.println("Hi there");
    }
}

Now we got a program that prints Hi there on the screen.
Go to File > Save.
Now one important thing!
The default file format for Notepad is .txt.
However, java source code file format is, as we all probably know, .java.
So basically we’ve to save the file as test.java so that javac (The Java Compiler) can recognize it as a Java Source File, rather than a text file!
For simplicity we’re going to save this file in C: drive.
So the file location is now C:\test.java

Now, this part can be a little tedious!
First of all, you’ve to find the JDK installation directory.
If you’re on Windows, then go to
 C:\Program Files\Java\jdk1.*****\bin
In place of “jdk1.*****” you just have to put your JDK version. For instance mine is, jdk1.8.0_65
So the full path to my java directory is
C:\Program Files\Java\jdk1.8.0_65\bin

If you’re running Linux, check one of these following locations.
 /usr/lib/jvm
/usr/local
/usr/bin

Hope you got the point!
Now open cmd from start menu with “Run as Administrator”!
Now type this command in cmd,
cd C:\Program Files\Java\jdk1.*****\bin
then the output should be like this picture!


Notice that we’ve changed the current working directory from
C:\Windows\system32  (green rectangle)
To
C:\Program Files\Java\jdk1.8.0_65\bin  (red rectangle)
So that we can run the program javac.exe (as javac is in the JDK installation directory, rather than C:\Windows\system32\ directory!)
The cd command (cd stand for change directory) just changed the directory from C:\Windows\system32\ to C:\Program Files\Java\jdk1.8.0_65\bin.

Now to the fun part!
Let’s compile the test.java file manually!
In the cmd, type this command,
javac C:/test.java
The compilation process should end without showing anything like below!



Now there should be a new file called test.class in C: drive.
It contains all the compiled Java Byte Codes for the test.java source file.

Now we need to run it using JVM (Java Virtual Machine).
We have to change the current working directory using cd command again.
Now we’ve to write,
cd C:\
Now we just have to call the JVM and pass the .class file.
We do it like below,

java test
Note that we didn’t mention test.class because JVM can only run compiled code, that’s the class file in this case!
So, when we wrote test in the command it searched for all the test file with .class extension.
Then it found the test.class file and run it!

So the output will be like below,



It should print

Hi there

On the screen!

For Linux,
Open terminal and go to the directory containing the test.java file,
then type,
javac test.java
Now the file is compiled and the .class file is generated. Now simple run it with,
java test
Now it should print the output in the Terminal.

That’s it!
Now you know how to compile and run a java program manually!


No comments

Powered by Blogger.