97

I've compiled a HelloWorld program, and I'm using the command prompt to run it. The .class file is named HelloWorld2.class

The file is located in C:\Users\Matt\workspace\HelloWorld2\bin Here's what I'm getting when I go to command prompt, and type "Java HelloWorld2" :

C:\Users\Matt>Java HelloWorld2
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld2
Caused by: java.lang.ClassNotFoundException: HelloWorld2
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: HelloWorld2.  Program will exit.

I was expecting to see a HelloWorld printed out. What am I doing wrong? I have the JDK installed.

Jonathon Faust
  • 12,396
  • 4
  • 50
  • 63
Skizz
  • 1,405
  • 2
  • 11
  • 12
  • 10
    In addition to Isaac's answer to your problem. It may be a good idea to write "java" in lower case because upper case only works on MS Windows. – Hendrik Brummermann Apr 22 '11 at 15:53
  • Just a note that Java 11 onwards has support for running directly from a single `.java` file. So you can just run `java HelloWorld2.java` – John Gilmer Nov 01 '22 at 11:34

4 Answers4

113

If your class does not have a package, you only need to set the classpath to find your compiled class:

java -cp C:\Users\Matt\workspace\HelloWorld2\bin HelloWorld2

If your class has a package, then it needs to be in a directory corresponding to the package name, and the classpath must be set to the root of the directory tree that represents the package.

// Source file HelloWorld2/src/com/example/HelloWorld2.java
package com.example;
...

Compiled class file: HelloWorld2/bin/com/example/HelloWorld2.class

$ java -cp HelloWorld2/bin com.example.HelloWorld2
GreenGiant
  • 4,930
  • 1
  • 46
  • 76
Isaac Truett
  • 8,734
  • 1
  • 29
  • 48
  • It works perfectly from anywhere in my file structure. Simply put the path to the compiled class into a -cp param and then the compiled class name without any path. You may also run this class without the -cp param if you are in the same folder as your compiled class. – Václav May 04 '20 at 10:41
  • 2
    @Václav Setting the path directly to the folder with the class file only works in cases without a package. Most projects should define a package so the classpath must be set to the root of the compiled structure, the `bin` in answer. – Annan Yearian Jul 15 '22 at 14:23
  • @AnnanYearian that's right – Václav Jul 25 '22 at 20:01
52

To run Java class file from the command line, the syntax is:

java -classpath /path/to/jars <packageName>.<MainClassName>

where packageName (usually starts with either com or org) is the folder name where your class file is present.

For example if your main class name is App and Java package name of your app is com.foo.app, then your class file needs to be in com/foo/app folder (separate folder for each dot), so you run your app as:

$ java com.foo.app.App

Note: $ is indicating shell prompt, ignore it when typing

If your class doesn't have any package name defined, simply run as: java App.

If you've any other jar dependencies, make sure you specified your classpath parameter either with -cp/-classpath or using CLASSPATH variable which points to the folder with your jar/war/ear/zip/class files. So on Linux you can prefix the command with: CLASSPATH=/path/to/jars, on Windows you need to add the folder into system variable. If not set, the user class path consists of the current directory (.).


Practical example

Given we've created sample project using Maven as:

$ mvn archetype:generate -DgroupId=com.foo.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 

and we've compiled our project by mvn compile in our my-app/ dir, it'll generate our class file is in target/classes/com/foo/app/App.class.

To run it, we can either specify class path via -cp or going to it directly, check examples below:

$ find . -name "*.class"
./target/classes/com/foo/app/App.class
$ CLASSPATH=target/classes/ java com.foo.app.App
Hello World!
$ java -cp target/classes com.foo.app.App
Hello World!
$ java -classpath .:/path/to/other-jars:target/classes com.foo.app.App
Hello World!
$ cd target/classes && java com.foo.app.App
Hello World!

To double check your class and package name, you can use Java class file disassembler tool, e.g.:

$ javap target/classes/com/foo/app/App.class
Compiled from "App.java"
public class com.foo.app.App {
  public com.foo.app.App();
  public static void main(java.lang.String[]);
}

Note: javap won't work if the compiled file has been obfuscated.

kenorb
  • 155,785
  • 88
  • 678
  • 743
2

This can mean a lot of things, but the most common one is that the class contained in the file doesn't have the same name as the file itself. So, check if your class is also called HelloWorld2.

Federico Vera
  • 1,357
  • 1
  • 12
  • 18
-5
  1. Go to the path where you saved the java file you want to compile.
  2. Replace path by typing cmd and press enter.
  3. Command Prompt Directory will pop up containing the path file like C:/blah/blah/foldercontainJava
  4. Enter javac javafile.java
  5. Press Enter. It will automatically generate java class file
Arcones
  • 3,954
  • 4
  • 26
  • 46
odunstory
  • 1
  • 2