Ubuntu: Runing a java program, Oracle JDK installed [closed]


I recently install Oracle JDK, set Environment variables. Everything works fine.

java -version on terminal

java version "1.8.0_131"  Java(TM) SE Runtime Environment (build 1.8.0_131-b11)  Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)  

I have a simple Hello.java program I will like to run on terminal.

package com.company;    public class Hello {        public static void main(String[] args) {      System.out.println("hello, world");      }  }  

I compile I get my Hello.class file generated, in the .../out/.../com/company/..

Runs fine in my IDE but I will Eventually have to run form terminal. cd-ed to Hello.class directory.

java Hello  

gives the following error massage.

Error: Could not find or load main class Hello  

How can I get it to run from terminal.


Copy the following into a script called Hello.java:

package com.company;  public class Hello {      public static void main(String[] args) {      System.out.println("hello, world");      }  }  

The -d option in the following command creates the appropriate directory hierarchy. Compile your Java program with: javac -d . Hello.java then run it with:

java com.company.Hello  


The problem generating the error

Java looks for your application in the path specified by your package directive. In this case com.company.

You can compile that simple program by commenting out the package directive, compile it without the package, or create the path and call it by the full path.

$ mkdir -p com/company  $ mv Hello.class com/company/  $ java com/company/hello  hello, world  

If you remove the line package com.company; by commenting it out it will work as you have already tried and expect.

If you have a large project with multiple folders for your class files you can use a jar manager program or the command line version jar to create a Hello.jar file.

Your IDE is providing the path and other details specified above. You can manually create this environment for running your commands (as well as having multiple classes and packages in your archive) using the jar command.

Using the Jar command to create jar programs

You can create your Hello application to run as intended and have the type of path included using this method:

Create the path

$ mkdir -p com/company  

Place your class files in the path of the package line:

$ mv Hello.class com/company  

Create a manifest file to indicate the main class location


Manifest-Version: 1.0  Main-Class: com.company.Hello  

Run the jar command to create the package

$ jar cfm test.jar manifest.txt com/

You will now have a new file created, called test.jar. The second argument could have been named anything including Hello.jar or just hello. The new file can be run in a number of ways:

$ java -jar test.jar  $ ./test.jar  

If you have created the jar file by with the second argument being hello, you could run it with:

$ ./hello  

You could also rename a java.jar file to any name after it's created and run it by that name.

Running a Java class is very restrictive in that it has to be run by the class name by which it was compiled.

To run a jar file as a command without the "-jar" argument you would have to change the permission bit (chmod +x Hello.jar).

Jar Command Details

The jar arguments in this example are:

      -c  Create a new archive      -f  Specify the archive (jar filename)      -m  Include manifest information from the specified manifest file  

Jar is installed along as part of the JDK package. You can get more details of using from the man pages (man jar) as well as a quick help by running it without an argument.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »