Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run Java .class files?

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.

like image 625
Skizz Avatar asked Apr 22 '11 15:04

Skizz


People also ask

What program will open a .class file?

CLASS files can be compiled from JAVA files using the javac command, which is included with a JVM installation. Many Java IDEs, such as Eclipse, can compile CLASS files on the fly as developers write program code.

How do you compile and run a Java program?

Type 'javac MyFirstJavaProgram. java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set). Now, type ' java MyFirstJavaProgram ' to run your program.

How do I run a Java program in classpath?

Setting Classpath from Command LineUse set CLASSPATH command initially, and then run Java application or tool in the same command line window. “ It will search the classes/resources in mentioned classpath locations. Classpath entries that are neither directories nor archives (.


2 Answers

You need to set the classpath to find your compiled class:

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

like image 136
Isaac Truett Avatar answered Oct 07 '22 20:10

Isaac Truett


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.

like image 30
kenorb Avatar answered Oct 07 '22 20:10

kenorb