How do I run Java .class files?

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

Leave a Comment