Bobby
Bobby

Reputation: 97

Can you run a .class file from terminal that is outputted by an IDE

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)

Upvotes: 0

Views: 14332

Answers (6)

nino
nino

Reputation: 1

according to terminal ide, android requires classes in DEX format when running them.

Try:

dx --dex --output=practice.jar practice.class

Then run using this:

java -jar practice.jar practice

Upvotes: 0

Sriharshaa
Sriharshaa

Reputation: 1

Just consider this example

say I already have a folder src and I wrote in my notepad

package test.oye;

class testclass {
static public void main (String [] args)
{
int a=3;
System.out.println(a);
}
}

then what go to src folder and you ,yourself create a folder named test and inside it oye . Then put your .java file in it . Then cd src/test/oye only(in Command prompt or terminal).From there itself

javac testclass.java
cd src
java test.oye.testclass

This will work for sure.

If you don’t want to put .java file there … then just compile your .java file and get the .class file . Now create the test folder and then oye inside it ….and put .class file inside it ….

Now go back to src …and then type

java test.oye.testclass;

Upvotes: 0

KevinDTimm
KevinDTimm

Reputation: 14376

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

Upvotes: 1

Tomas Narros
Tomas Narros

Reputation: 13488

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).

Upvotes: 2

Andrzej Doyle
Andrzej Doyle

Reputation: 103837

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!)

Upvotes: 0

Mark Baijens
Mark Baijens

Reputation: 13222

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.

Upvotes: 0

Related Questions