Compile our app using javac Java Compiler. This operation will translate our code that we can read to the code that Java Runtime Environment (JRE) can execute. JRE is a program that can run Java programs. It contains Java package classes, runtime libraries and Java Virtual Machine JVM.
javac -d . App.java
Run the app
Type ls to list files in the in the hello-world. There will be two files:
App.class App.java
Now, we can run our app using JRE by typing:
java -classpath . App
The output is going to be the following:
Hello, World!
The second parameter -classpath . means that Java should search for the class files in the current directory. We also should pass the class name App that contains main method.
Create executable JAR file
In the next step we’ll create an executable Java Archive File (JAR) file. The JAR file contains all the resources that are needed to run the app such as class files, configuration files, images, and other resource files.
The only file we need to create is a Manifest file. It is a configuration file that will contain information about a class that contains main method.
Create App.mf manifest file by typing:
cat > App.mf
And pasting the following text:
Manifest-Version:1.0Main-Class:App
When the JRE will execute our application it will look for the class name that contains application entry point main method. In our case the entry point is in App class.
We’re ready to create our JAR file, type the following
jar cmf App.mf App.jar App.class App.java
This command will create App.jar file and place it in the current directory.
Let’s now run the JAR file that we’ve created by typing.
java -jar App.jar
The output is going to be the following:
Hello, World!
The file App.jar can be shared and executed on another machine with JRE installed.
Summary
In this article we’ve created sample Java application that prints “Hello, World!” to the console.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.