I've written a program to read in data from a text file and do some simple calculations, then print out those calculations. That part works great.
Afterward, I added in some code to do a t-test, using the TTest class (org.apache.commons.math3.stat.inference.TTest). So, I downloaded commons-math3-3.6.jar from the Apache Commons download page and put the JAR file in the same folder as the rest of my Java code for this program.
I use the following command in Windows to compile, which works fine:
javac -cp ./commons-math3-3.6.jar ./FootballTeam.java ./Main.java
But I can't figure out how to correctly run the program. I've tried this:
java Main
which executes everything up to the t-test perfectly, and then gives the expected error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/math3/stat/inference/TTest
at Main.main(Main.java:32)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.stat.inference.TTest
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)
... 1 more
I've also tried this:
java -cp commons-math3-3.6.jar Main
which gives me this:
Error: Could not find or load main class Main
I cannot for the life of me figure out how to properly set the classpath here. Can someone provide me some assistance? Thank you!
The Main class cannot be found because the current directory (.) is not on your classpath.
To add it, on Windows:
java -cp ".;commons-math3-3.6.jar" Main
On *n?x:
java -cp ".:commons-math3-3.6.jar" Main
Related
I'm a newbie to Java (just two days) and am trying to get an HTTP server running using Spark. I created a new Maven project in IntelliJ and have a Hello World response coming back from Spark when running it in IntelliJ. As a learning exercise, I would like to get the app running from the command line using Java.exe. When I run java Main, I get the following error:
Exception in thread "main" java.lang.BootstrapMethodError:
java.lang.NoClassDef oundError: spark/Request
at Main.main(Main.java:5) Caused by: java.lang.NoClassDefFoundError: spark/Request
... 1 more Caused by: java.lang.ClassNotFoundException: spark.Request
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)
... 1 more
Looks like it can't find the Spark .jar files. There's a command line option -cp to specify paths to dependencies. After looking up where Maven keeps these files, I tried running:
java -cp .;C:\Users\jbuschmann.m2\repository Main
Still same error. How do I specify the path to the Maven dependencies?
Instead of giving the repository in the -classpath, try adding the location of your spark.jar.
java -cp \path\to\spark.jar Main
So, I am afraid that I have to join the crowds flocking here and asking: "What's wrong with my code?"
I've recently started coding in Java and I've had none of this error until I tried using lwjgl in a project. So, the question is: What is wrong with my JAR when I get the following:
java -cp \lib\win32\lwjgl.jar -jar Valor.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/opengl/Display
at bin.Disp.createWindow(Disp.java:18)
at bin.Thread.gameLoop(Thread.java:13)
at Main.main(Main.java:4)
Caused by: java.lang.ClassNotFoundException: org.lwjgl.opengl.Display
at java.net.URLClassLoader$1.run(Unknown Source)
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)
... 3 more
As is, I am extremely confused about this. I've gone through seven or eight posts here about this very problem, and another five or six on other web sites, yet none of the methods those people used seem to work for me.
Currently, my file structure is:
Valor
-Valor.jar
lib
win32
-lwjgl.jar
-lwjgl.dll
Manifest:
Manifest-Version: 1.0
Main-Class: Main
I did try defining a classpath here... Didn't work. So, I'm pretty much as confused as I can possibly get. Thank you for your help.
Edit: This error doesn't occur while running in Eclipse, only when I export and try to run it myself.
Try to run it like this:
java -cp lib\win32\lwjgl.jar;Valor.jar Main
The problem is that the -cp option is ignored when -jar is used.
Alternativelly you could extend the class path by adding a Class-Path: entry in your manifest and puting lib\win32\lwjgl.jar in it.
Class-Path: lib\win32\lwjgl.jar
use this link to install ljgl and define your classPath : http://www.lwjgl.org/wiki/index.php?title=Setting_Up_LWJGL_with_Eclipse
By running java -cp \lib\win32\lwjgl.jar -jar Valor.jar you're telling Java to look for LWJGL classpath near the root of your filesystem. Seeing your directory structure, use:
java -cp .\lib\win32\lwjgl.jar -jar Valor.jar
Notice the . at the beginning.
I've just started learning Jython, and I'm having some issues with implementation. I've looked through the Demo files that come with Jython 2.5, and I'm still unsure how to implement Jython to get what I want.
Currently, I've got Java code executing a Python server as a Process. The Python server in turn starts a second section of Java code as a subprocess. I was originally going to replace the Python server with a Java NIO server, but that's given me no end of grief, and thus why I'm trying Jython. I also want to get all of this into a .jar.
I tried making a simple Python file (print "Hello World"). It runs with Jython, but when I try to run it using java (after doing jython -m compileall.) it says that it can't find main. I assume that I need to add something to my Python code to make it work, but I'm not sure what.
Edit: The exact error I'm getting is this-
Exception in thread "main" java.lang.NoClassDefFoundError: jythonTest
Caused by: java.lang.ClassNotFoundException: jythonTest
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: jythonTest. Program will exit.
I think the answer is CLASSPATH.
hello.py
print "Hello"
Let's run it
> python hello.py
Hello
> jython hello.py
Hello
Compile (I used py_compile to compile single file)
> jython -m py_compile hello.py
Run with java
> java -classpath d:\P\jython253\jython.jar;. hello$py
Hello
Note the dot in classpath. It is required for java to find your compiled class in current directory.
I have a java file which uses jfreechart libraries, uses a text file from local drive and displays graph. Runs fine with eclipse. However, I want to run this from cmd prompt, other simple Java files are able to run successfully via cmd prmnt but not able to run this file.
PS: MyTool.java is able to compile without errors and class file is created, but not able to run.
1) This is how I am compiling it in cmd prompt: (gives 0 errors)
C:\Documents and Settings\hello.maga\workspace\MyTool\lib>javac -cp "gnujaxp.
jar;iText-2.1.5.jar;jcommon-1.0.16.jar;jfreechart-1.0.13.jar;jfreechart-1.0.13-e
xperimental.jar;jfreechart-1.0.13.jar;junit.jar;servlet.jar;swtgraphics2d.jar" MyTool.java
2) This is how I am running it:
C:\Documents and Settings\hello.maga\workspace\MyTool\lib>java -cp "gnujaxp.j
ar;iText-2.1.5.jar;jcommon-1.0.16.jar;jfreechart-1.0.13.jar;jfreechart-1.0.13-ex
perimental.jar;jfreechart-1.0.13.jar;junit.jar;servlet.jar;swtgraphics2d.jar" MyTool
Error for second command:
Exception in thread "main" java.lang.NoClassDefFoundError: MyTool
Caused by: java.lang.ClassNotFoundException: MyTool
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: MyTool. Program will exit.
What I don't understand is, if there are any errors, then it should not compile in first place, can someone educate me. Thank you very much.
You need to include "." in the classpath, like so:
java -cp ".;gnujaxp.jar;iText-2.1.5.jar;jcommon-1.0.16.jar;jfreechart-1.0.13.jar;jfreechart-1.0.13-experimental.jar;jfreechart-1.0.13.jar;junit.jar;servlet.jar;swtgraphics2d.jar"
From "Setting the class path": "The class path tells SDK tools and applications where to find third-party and user-defined classes -- that is, classes that are not Java extensions or part of the Java platform. The class path needs to find any classes you've compiled with the javac compiler -- its default is the current directory to conveniently enable those classes to be found."
However if you set the classpath yourself, the default no longer applies, and you're expecting it to load classes from the current directory. You'll have to add it manually, like by adding "." to the class path as Ed Staub recommended.
When compiling, your class wasn't needed on the class path, so to speak, since it's what was being compiled. You only needed all the other classes (in the jar files) on the class path for that. That's why you're able to compile but not run, even though you used an identical class path for both operations.
So i need to write a program that accesses and modifies an SQLite DB for a school program and am looking at the basics firstly. Now, I am looking at this to get me started: link text. The problem is that when i compile Test.java (The example code on the website) and then run the command:
java -cp .:sqlitejdbc-v056.jar Test
like it tells me to, i get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
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)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: Test. Program will exit.
Test.java, Test.class and sqlitejdbc-v056.jar are all in the same folder so they should find each other. Does anybody have an idea about what i am doing wrong?
According to the java.lang.NoClassDefFoundError: Test, the Test class cannot be found in the CLASSPATH. But you wrote that Test.class is available in the current directory so I'm gonna guess something. Actually, I think that you are not using the right CLASSPATH separator character (which is platform dependent) and thus not building correctly the CLASSPATH. On Windows, you should use ; instead of :. If this happens to be the case, you should try this:
java -cp .;sqlitejdbc-v056.jar Test
If you are on a unix-like platform, forget this answer, the problem is elsewhere.
As the exception message is saying, the problem is that it cannot find the class Test. So the problem is not finding SQLite, but finding your Test class.
Just a guess, but I am wondering if there is an issue with how you compiled. In the Test.java, does the class belong to a package? If so, you need to compile it using the -d flag so that it gets put in the appropriate sub-directory.