Compile, Package and Run Java program in command line

By | 2018 年 4 月 15 日

Using Java IDE is simple for compile, package and run Java program. The IDE will take care of classpath details. However, if we want to run Java program with command line, it’s quite difficult at the first time.

For example, you may easily run into errors like:

Error: Could not find or load main class XXX
Caused by: java.lang.ClassNotFoundException: XXX

Or

Error: Could not find or load main class XXX
Caused by: java.lang.NoClassDefFoundError: XXX (wrong name: XXX)

This guide will go through a working example for the whole process and solve the above problems.

The testing Java program

We first build a Java program with just 2 classes, as:

src
├── Main.java
└── net
    └── unieagle
        └── Helloworld.java

In Main.java

public class Main {
    public static void main(String[] args) {
        System.out.println("From Main");
        if (args.length > 0)
            System.out.println(args[0]);
    }
}

In Helloworld.java:

package net.unieagle;
public class Helloworld {
    public static void main(String[] args) {
        System.out.println("From Helloworld");
        if (args.length > 0)
            System.out.println(args[0]);
    }
}

We can have multiple classes defined, but for entry point, we need each class has a public static void main defined.

Compile them

Compile java files into class files with javac command:

javac -d ./target src/net/unieagle/*.java src/*.java

Then it will generate files like:

target
├── Main$A.class
├── Main.class
└── net
    └── unieagle
        └── Helloworld.class

Running Java from class file

At this point, we can run the program with java

$java -cp target Main
From Main

$java -cp target net.unieagle.Helloworld
From Helloworld

Or if we cd into the target directory:

$pwd
(...)target

$java Main
From Main

$java net.unieagle.Helloworld
From Helloworld

However, if we forgot the -cp, it will cause problem:

$java Main
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main

Or if we forget the package name for Helloworld:

$java -cp target Helloworld
Error: Could not find or load main class Helloworld
Caused by: java.lang.ClassNotFoundException: Helloworld

Or if we are in the wrong place:

$pwd
(...)target/net/unieagle

$java Helloworld
Error: Could not find or load main class Helloworld
Caused by: java.lang.NoClassDefFoundError: net/unieagle/Helloworld (wrong name: Helloworld)

$java net.unieagle.Helloworld
Error: Could not find or load main class net.unieagle.Helloworld
Caused by: java.lang.ClassNotFoundException: net.unieagle.Helloworld

Package in Jar

In order to package a working Jar, we need to cd into the target directory. Which is the ‘root’ dir for our Java application compiled output. Or we will get the Error: Could not find or load main class XXX error when we want to run the class in the Jar.

$pwd
(...)target

$jar cvf test.jar ./                      
added manifest
adding: net/(in = 0) (out= 0)(stored 0%)
adding: net/unieagle/(in = 0) (out= 0)(stored 0%)
adding: net/unieagle/Helloworld.class(in = 560) (out= 400)(deflated 28%)
adding: Main.class(in = 582) (out= 419)(deflated 28%)
adding: Main$A.class(in = 245) (out= 198)(deflated 19%)

It will generate a test.jar file there.

Run program from the Jar

$java -cp test.jar Main
From Main

$java -cp test.jar net.unieagle.Helloworld
From Helloworld

发表评论

电子邮件地址不会被公开。 必填项已用*标注