views:

58

answers:

4

I am trying to run a file from command line. The file is a .class file and is apart of a larger project that I compiled in Netbeans. I navigated to the .class file and ran

java MyFile

And I got:

Exception in thread "main" java.lang.NoClassDefFoundError: PersonTest/class
Caused by: java.lang.ClassNotFoundException: PersonTest.class
   at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.net.URLClassLoader.findClass(URLClassLoader.java:205) 
   at java.lang.ClassLoader.loadClass(ClassLoader.java:321) 
   at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) 
   at java.lang.ClassLoader.loadClass(ClassLoader.java:266)

Could not find the main class: PersonTest.class. Program will exit

Whats up with that? (I should mention that i'm running ubuntu)

A: 

Yes you can, they are compiled by a java compiler. If you have the right version of the jvm (often other versions work aswell) than it can be run. The information about your error is not enough to tell what went wrong. Your probably in the wrong folder, mistyped the classname, used a class in your code that couldn't be found, etc.

Mark Baijens
A: 

You need to check this useful link java - the Java application launcher:

By default, the first non-option argument is the name of the class to be invoked. A fully-qualified class name should be used

So, you have to write the full qualified name of the class (this includes the package name).

So, the right way to execute your command is this (from the root dir where your class files are stored):

> java my.package.MyFile

Also, make sure to include all the needed dependencies at the classpath (-cp) argument (check the referenced link).

UPDATE: to include a classpath setting example:

java -classpath C:\MyProject\classes;C:\MyProject\lib\utility.jar my.package.MyFile

With this, the java runtime will search for the classes at the C:\MyProject\classes directory, and at the C:\MyProject\lib\utility.jar JAR file. You'll need not only your class direct dependencies, but the dependencies needed by the referenced files (the whole tree).

Tomas Narros
tried that, got a similar looking error though
Bobby
Have you run it from the root classes dir? And set the dependencies at the classpath?
Tomas Narros
so, if this file imports 4 other files then i need to pass paths to these 4 files?
Bobby
No. You have to tell the java command where to search those dependencies. See my update.
Tomas Narros
Too bad he accepted an answer that will not solve his problem. Yes, there is a lot of important information, but it doesn't contain the solutions. See his stack trace and see my answer to see why.
KevinDTimm
@KevinDTimm: The stack trace was not available when I wrote my answer, and still, giving him just some useful information has helped him to solve his problem. "Show me to fish instead giving me fishes"
Tomas Narros
@Tomas - 100% agreed, my comment is aimed at the OP. Fishing rather than fishes is the best way and kudos for doing so.
KevinDTimm
A: 

Unless your class is entirely standalone (i.e. only references java.lang classes like String), you'll need to add other classes/JARs to the classpath when you invoke Java.

The NoClassDefFoundError (which usually states the name of the class by the way, and always includes a stacktrace) indicates that an external class that was available when your class was compiled, is not available on the classpath at runtime.

EDIT based on update:

You're invoking your process incorrectly. You don't need to append the .class suffix of the file - doing so makes Java look for a file class class in a subpackage.

(P.S. you said you ran java MyFile. That's a lie, you actually ran java PersonTest.class. If you'd noted that to start with, it would have made it much easier for people to answer the question!)

Andrzej Doyle
A: 

The answer appears to be in this line:

Exception in thread "main" java.lang.NoClassDefFoundError: PersonTest/class

It means you didn't type:

java MyFile

as you said in your original post, you typed

java PersonTest.class

you should have typed

java PersonTest
KevinDTimm