Java is the object-oriented programming language, and it is the computing platform. Many programming languages allow a developer to compile and write a program in the same environment. But the best feature of Java programming language is to write a program once and run that program on multiple platforms. Java allows you to run a program in Command Prompt also. The Command Prompt version changes with Operating System like Windows and Mac. In Windows, you can run and compile a program using Command Prompt, but in Mac, you can run and execute a program using the Terminal. The process of executing a program is same on both OS. You can find freelance jobs online in this sector easily.
How to run simple Java program on Terminal?
Create a temporary folder in c drive such as c:\filename. Using the text editor or through Notepad create small Java program. Here you find the example of HelloWorld.java program. You can hire freelancers who have knowledge and experience of working with Java programming language.
Public class HelloWorld
{
public static void main(String[] args)
{
System.out.println(“Hello, World!”);
}
}
After the creation of the program mentioned above, you will save that program in an already created folder with .java extension. You can give any filename but ensure that extension should always be .java.
Open terminal/ command prompt: The way of accessing command line in Mac and Windows is different.
For Windows: Go to Home, then type cmd and enter to open the Command Prompt.
Mac: In Mac press the Go tab, select Applications, select Utilities, then select Terminal.
Type Java-version on your command line. You can see the current version of Java installed on your system. If Java is not available on your system, then you have to download and installed Java Development Kit from the website.
For changing the directory use the command “cd” on command line such that if you are operating in C:\Users\Project and need to get to C:\Users\Project\NewProject. Enter in cd NewProject and Press Enter. You can see the list of the current directory by pressing dir and pressing the Enter.
After correction of your directory, you can compile the Java program by simply typing javac filename.java into a command line and then press Enter. If a program has some errors, then it notifies you. Otherwise, it does not display any errors.
Enter” java filename” and press Enter to execute the program where your program should run successfully. Suppose you face some problem then use the method of troubleshooting.
Suppose you are running the simple program then you find your entire file in a single directory and suppose you are working with a complex program then your program file is found in multiple directories. In such a case you have to tell your computer where to look for this file.
If you are using Windows operating system, then type java –version on to command prompt and press Enter. According to your version of Java set path such as path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin to the command prompt and then press Enter key. Replace jdk1.5.0_09 with whatever version you have installed.
If you are using the Mac operating system then enter /Usr/libexec/ java home-v 1.7 on to the terminal and press Enter. Now type echo export “JAVA_HOME=\ $ (/usr/libexec/java_home)” >> / .bash_profile into the terminal and press Enter and then restart terminal.
How to run Applet program on Terminal?
Open a terminal and navigate your Java project directory.
Cd MyNewProject
For editing the project file using one of the text editors and save it. It is the applet program so you will require the HTML file to run on appletviewer. Now open a text editor to create and edit HTML file for your Java class and save your Java file as a .html. You can hire freelancers who know about creating Java program using the applet.
<html>
<p> This file launches the ‘YourJavaClass’ applet: YourJavaClass.class! </p>
<applet code=”YourJavaClass.class” height=200 width=320>
Your browser does not recognize the applet tag.
</applet>
</html>
Now save this file and compile it on a terminal. After compilation of the program, you have to enter javac YourJavaClass.java. Suppose you find any error in your program then correct it. If you want to compile the whole program, then hit ‘Enter’.
javac *.java.
To launch applet use the appletviewer. The program appletviewer work on HTML and that will help you to run applet outside of the context of a web browser. Enter this command on terminal.
Appletviewer YourJavaClass.html
Summary:
In this article, you find the complete information on how to compile and run Java program through terminal. You also find information regarding how to compile and run Java applet program on a terminal. Execution of both programs is same, but the only difference is that in applet program you require HTML file for execution.