When I try to do a simple "Hello World" java application I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: /path/to/mainarg*
Caused by: java.lang.ClassNotFoundException: first.Skeleton
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
I am running on Mac OS 10.8.1 and am running Java 1.6. I have checked my run configurations to make sure the main argument can be found. For the sake of simplicity I have also tried to use the default JRE of java 1.6 that is included with mac. The build configuration I have used is also correct and pointed to the right place.
When I followed this tutorial online, and executed the commands over the terminal I get the same error. What am I missing? I don't think it is an Eclipse problem anymore, but something bigger. Any advice would be greatly appreciated.
Here is the code I tried to run:
public class Test{
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String[]args){
System.out.println("Hello World");// prints Hello World
}
}
I put the code on my Desktop for simplicity. I went to /usr/tyler/desktop then made the script an executable with "javac Test.java", and when I tried to run it with "java Test.java" I then got the error.
Here are the other tutorials I have followed:
-Java Compilation error on Mac
-HelloWorld with Eclipse on Mac
-Along with many others from SO and elsewhere
EDIT:
Thank you for the help. The terminal issue has been resolved. However, if anyone has any advice on what could be going wrong with Eclipse, that would be appreciated.
You need to run ...
"java Test"
You cannot execute the java code, you need to execute the compiled class file. The java executable will look for a .class file with the name Test.
execute or run
java Test
instead of
java Test.java
Related
I can see that this question has been asked before, but I have not found previous answers useful - specifically my issue is following Princeton University's 'Algorithms' course on Coursera here and using their algs4.jar file to access ostensibly necessary utilities. I have never used Java before but this seemed like such a good course, I'm trying to muddle through rather than switching to a 'worse' course in a language I know. I have also found a thread specifically on this issue here and a Reddit question here, which were equally as useless as the other questions which had this problem, but not with the Princeton's algs4.jar file.
This is my code, I haven't bothered to finish it but it should run. Before anybody asks, I've commented out everything except the first if (StdIn.isEmpty()) check and it still gives me all the same compiler/interpreter errors, so this is nothing to do with it being half-finished - StdIn.isEmpty() and StdIn.readString() are supposed to be provided by algs4.jar:
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
class RandomWord
{
public static void main(String[] args)
{
if (StdIn.isEmpty())
{
System.out.println("Nothing to read.");
return;
}
String curr_selected_word;
double curr_probability = 0.0;
int i = 0;
while (!StdIn.isEmpty())
{
String current_string = StdIn.readString();
System.out.println("> " + current_string);
}
}
}
Following the advice of previous threads, I compile like this:
javac -cp "./algs4.jar" RandomWord.java
Which works fine. Running java RandomWord, I get this large messy error:
Exception in thread "main" java.lang.NoClassDefFoundError: edu/princeton/cs/algs4/StdIn
at RandomWord.main(RandomWord.java:13)
Caused by: java.lang.ClassNotFoundException: edu.princeton.cs.algs4.StdIn
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more
The accepted answer on the other question was to reference the .jar file explicitly, like this:
java -cp "C:\Coursera\Algorithms\Part 1\Java Solutions\HelloWorld;C:\Coursera\Algorithms\Part 1\Java Solutions\algs4.jar" RandomWord
Which gives me the exact same error. There is also a comment on the Reddit thread that uses the -classpath instead of -cp flag, which at least gives a different but stranger error:
>java -classpath ./algs4.jar RandomWord
Error: Could not find or load main class RandomWord
Caused by: java.lang.ClassNotFoundException: RandomWord
Putting quotes around the .jar filepath, or providing the absolute filepath, all produce the same error.
I Googled this and predictably it is caused by not having a main entry-point, but... see code above?
Lastly, the installer for the Coursera course apparently installs some Bash commands (see here) called javac-algs4 and java-algs4 which is supposed to sort out all this classpath nonsense for me. So I crack open Bash, and:
>javac-algs4 RandomWord.java
'javac-algs4' is not recognized as an internal or external command,
operable program or batch file.
I have restarted since running the installer. I would add these to my PATH manually, but I don't even know where they are; the C:\Program Files\LIFT-CS folder that their installer installs into only contains a pissing uninstaller. So I'm absolutely losing my rag and at my wits' end after spending over 7 hours just trying to start this course, not even getting stuck on the problems or the content. When will content creators learn we don't want your in-house IDE with no dark mode and libraries that do nothing but rename every function to camel-case?
Anyway, if anyone has encountered this or knows what I could do to fix it, help would be appreciated.
You can try this -
javac -cp .;<insert class path> RandomWord
I was facing the exact same issue, I ran javac -cp .;.lift/algs4.jar RandomWord and it works. (My compiled class was in the same directory as .lift)
This worked for me on a Mac. Copy algs4.jar to the root of your project, then:
javac -cp ".:./algs4.jar" RandomWord.java
java -cp ".:./algs4.jar" RandomWord
I have a java gradle projectA that references another java gradle projectB, that builds as a lib.
My gradle build configuration seems to be fine, since I can import and use classes from the other project, and it compiles. But when I try to run the application from the command line I get an error...
Exception in thread "main" java.lang.NoClassDefFoundError:
dawcore/SamplerInstrument at DawCLI.main(DawCLI.java:17) Caused by:
java.lang.ClassNotFoundException: dawcore.SamplerInstrument at
java.net.URLClassLoader.findClass(URLClassLoader.java:382) at
java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at
java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more
It seems to be complaining that it can't load the classes in the jar at runtime....which makes sense. But I don't know how to have it successfully load those classes.
my current run command that does not reference the jar, is as follows....
/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/java -ea -cp "build/classes/main/" DawCLI
Running this gives the initially mentioned error.
I then read the docs on java -cp argument. It says to provide additional classpath directories, to separate them by semicolon.
I susequently updated my run command to be as follows....
/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/java -ea -cp "build/classes/main/;../DawCore/build/libs/DawCore.jar" DawCLI
This gives me the following error....
Error: Could not find or load main class DawCLI
My main function is as follows..
import dawcore.*;
public class DawCLI {
public static void main (String [] args) throws IOException {
SamplerInstrument samplerkick = new SamplerInstrument();
}
}
According to the docs I seem to be doing this correctly, but am still getting errors. Any insight on this would be greatly appreciated!
Thanks
The solution was to replace the delimiting semi-colon with a colon instead. Even though the first line of the javadocs on -cp command indicate that you should use a semi-colon.
This could be because I am running on linux and those docs indicate Windows.
https://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html
I found a couple other questions with my problem, but the solutions did not work. I have a web page on my local server that you can input text for a whole java class. When submit is clicked I want the output to be displayed. Using shell_exec I can compile the java file. I get nothing when I try to run it, though.
shell_exec("javac /folder/Test.java"); // works
echo shell_exec("/usr/bin/java folder.Test"); // nothing is returned
I decided to use the full path to java when I read that it solved the problem for someone else. When I run:
/usr/bin/java folder.Test
from the command line it works.
The Test.java file looks like this:
package folder;
public class Test{
public static void main(String[] args){
System.out.println("testing");
}
}
EDIT: I could not get w0rldart's to work. I can actually see something when I try Mathieu's. I tried adding the -classpath option (I am not sure if I am using it correctly)
echo shell_exec("java -classpath ./folder/ Test 2>&1");
I get the same error as without -classpath:
Exception in thread "main" java.lang.NoClassDefFoundError: Test Caused by:
java.lang.ClassNotFoundException: Test 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:
Test. Program will exit.
IT WORKS:
Just had to remove the period from
echo shell_exec("java -classpath ./folder/ Test 2>&1"); //error
echo shell_exec("java -classpath /folder/ Test 2>&1"); //works
I'm very uncertain this will make a difference. But just to be sure, have you tried adding 2>&1 after your command?
echo shell_exec("/usr/bin/java folder.Test 2>&1");
I don't think system.out.println is using stderr to display its output, but it might be worth trying anyway...
If you want to use Test class from folder package then Test.class needs to be saved in a folder named folder. Then you need to have folder's parent directory on the CLASSPATH.
So if your configuration is the following: /rootdir/foo/folder/Test.class then you can start Test like this: java -classpath /rootdir/foo folder.Test.
I think this behavior is the same if you use java from php's shell_exec.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
NoClassDefFoundError , Java
> java foo/boo/Prog
Exception in thread "main" java.lang.NoClassDefFoundError: foo/boo/Prog
Caused by: java.lang.ClassNotFoundException: foo.boo.Prog
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: foo/boo/Prog. Program will exit.
I'm really lost. I have a directory foo/boo/ and in that I have Prog.class along with some other dot-class files. foo is a subdirectory of the current directory where I run the command from. I compiled it fine, and in my foo/boo/ directory I've checked to make sure that there is indeed a "Prog.class" along with all the other dot-class files I need. There is a main method in my Prog class, and I'm pretty sure this problem has nothing to do with my source code (although it could of course) because I was able to run Prog fine in eclipse, just not from my terminal (ssh-ing onto another machine).
Could someone try to decipher what all of that jumble might mean? I don't really understand. Thank you very much.
$ java -h
Usage: java [-options] class [args...]
(to execute a class)
You are trying to specify the filesystem path to your class file, this is not possible. You need to specify the classpath correctly, so the class can be found by the classloader.
The classpath is a set of paths, where the java classloader looks for the classes to load. So specify the correct folders after the -cp parameter and it will be fine.
I'm probably misunderstanding (and don't have enough to just comment on questions) but are you saying you have a Prog directory, with a Prog.class in there? Wouldn't that make it foo.boo.Prog.Prog ?
Start from outside of foo/boo/Prog, i.e. having current directory being the parent of foo and run as #grtt1 said.
SAMPLE THAT WORKS
suzan#nebulla:~/Test_Java_01$ ls
foo
suzan#nebulla:~/Test_Java_01$ ls foo
boo
suzan#nebulla:~/Test_Java_01$ ls foo/boo
Prog.class Prog.java
suzan#nebulla:~/Test_Java_01$ cat foo/boo/Prog.java
package foo.boo;
public class Prog {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
suzan#nebulla:~/Test_Java_01$ java foo.boo.Prog
Hello world
I want to deploy a java appa on HEROKU. I am new to maven. I made all set up as per in the deploy a sample app to heroku (java+maven+ tomcat7)documentation.
and i made a bulid,It was successfull. But as soon i typed sh /target/bin/weapps i am getting the following error. Can anybody help me to sort out this issue.
Exception in thread "main" java.lang.NoClassDefFoundError: launch/Main
Caused by: java.lang.ClassNotFoundException: launch.Main</br>
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: launch.Main. Program will exit.
regards,
arunbv
I had a similar problem, I (well, almost) solved it this way, just remove:
<packaging>war</packaging>
from pom.xml. I know this is not an ideal solution, but somehow the embedded tomcat plugin doesn't work with war packaging!
I don't have enough points to comment, but what article are you following? Can you link it?
Based on the error, I suspect your Main.java is not in src/main/java/launch. If you follow these instructions, I think you should be able to clear up the error:
http://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat
If that's the article you're following and you're still having trouble, it may ease the problems you're having if you simply clone the sample repo:
git clone git://github.com/heroku/devcenter-embedded-tomcat.git
Since you said you are new to maven, I think the reason of this error is that you simply put your *.java code under the root folder of the project, while you are rather expected to put it under ./src/main/java/
This should work for you. I experienced the same problem and figured this out.
This is probably dead, but in case it helps people of the future.
I was getting up and running with a java app on Heroku, using an Ubuntu machine for development - and got this same Exception in thread "main" java.lang.NoClassDefFoundError.
Double check that you're using the same java compiler version as your java sdk version.
type java -version & javac -version. Make sure they are the same version.
If not, you can fix this by reconfiguring your environment to use the same for both.
Assuming you have downloaded and installed the java version you want to use, type :
`sudo update-alternatives --config java`
You'll get a nice menu with options
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 auto mode
1 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 manual mode
2 /usr/lib/jvm/jdk1.7.0/bin/java 1
Select the version you wish to use. Done.
Now try to build.