A few questions can be questioned when someone wants to learn Java programming. For example, how is its independent on working environment? How can we create a standalone program? How to run a Java program? etc. This post will answer a question among those. Although you are an experienced Java programmer, I suspect this post could be one of the favourite items in your programming handbook.
In this post, I want to make everything clearer by classifying a Java program into some phenomena.
First of all, I want to denote some conventional technical things in order to make this post more manageable and readable.
[box type=”shadow” ]The technical terms are used in this post
- java: name of Java program
- -cp : the option of Java program telling it run the jar file
- -jar : the option of Java program telling it run the jar file
- YourApp.jar : the jar file which you want to run with
- com.itersdesktop.javatechs.MainApp : the Fully Qualified Domain Name (FQDN) of the main class (entry point program). [tooltip text=”Notes” gravity=”nw”]If you don’t create any package, just replace the FQDN class with the main class name [/tooltip][/box]
A typical program written in pure Java programming language and compiled to a jar file can be generally run by executing the following command:
jar -jar YourApp.jar
But if you don’t have a manifest file in your jar, this command doesn’t work at all of the time. You will get an error telling no main manifest attribute, in YourApp.jar . If it is the case and you really want to run your program for whatever reasons, the following command can be helpful:
java -cp YourApp.jar com.itersdesktop.javatechs.MainApp

Otherwise, the following command allows to run
java -jar YourApp.jar com.itersdesktop.javatechs.MainApp
Here if you want to know how to create an executable jar file.
