The ClassNotFoundException questions are found by the dozens on SO but even so I did not find the answer to my problem in past answers :(
Basically I get a ClassNotFoundException while trying to run a large open source program from the command line using a command prompt that is provided in the project's doc. The prompt is as follow:
java -cp "target/classes/*;../../Algotrader/code/target/classes/*;../../lib/*;../../target/*" -Dsimulation=true -DdataSource.dataSet=1year com.algoTrader.starter.SimulationStarter simulateWithCurrentParams
(note: the original command actually says java.exe but I changed it to java as java.exe is not recognised as a command on my Mac)
The exception is thrown for the SimulationStarter class as shown by the stack trace:
Exception in thread "main" java.lang.NoClassDefFoundError: com/algoTrader/starter/SimulationStarter
Caused by: java.lang.ClassNotFoundException: com.algoTrader.starter.SimulationStarter
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)
From Eclipse I can see that SimulationStarter.class is in AlgoTrader/code/target/classes/com/algoTrader/starter, which looks in line with the path provided in the command prompt.
So my question is: what could be the cause(s) for this exception other than the class being incorrectly placed in the classpath?
Also not sure this makes any difference but the project is kept under svn and Maven and I am running it on a Mac.
CORRECT CLASSPATH
In the end the classpath given in the command prompt was at fault. The correct paths (at least ones that solve the problem) are:
java -cp "target/classes/:../../Algotrader/code/target/classes/:target/*" -Dsimulation=true -DdataSource.dataSet=1year com.algoTrader.starter.SimulationStarter simulateWithCurrentParams
The main differences with the original prompt is the removal of the stars except for the folder that contains the jar files and the shortening of the base classpath paths to capture only the base directories. Also ":" should be used instead of ";" on Macs and Linux as per
#reprogrammer answer
Are you sure you need all the * there?
Usually, you will want to give the base directories of the classpath, not subfolders.
E.g. when building your application into "bin", you would use java -cp bin mainclass, not java -cp bin/*! The support for * is generally a bit flaky, as it is a shell metacharacter, and you need to get quoting right. It can really screw you if you have an incorrect classpath. I've seen people have issues because they added README.TXT to their classpath.
The classpath syntax is OS-dependent. The classpath separator in Linux and Mac OS X is : not ;.
Related
I was trying to run a simple java program in windows by running the command
pmd -d path-to-source\com\vaannila\domain\User.java -f text -R java-basic
and error showing up is as below.
Exception in thread "main" java.lang.NoClassDefFoundError: C:\pmd\pmd-bin-5/4/6\bin//\lib\asm-5/0/4/jar
Caused by: java.lang.ClassNotFoundException: C:\pmd\pmd-bin-5.4.6\bin..\lib\asm-5.0.4.jar
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: C:\pmd\pmd-bin-5.4.6\bin..\lib\asm-5.0.4.jar. Program will exit.
Please help me with this to run the PMD
I can't reproduce your problem and I can't exactly make sense of the error message.
According to the error message, you have extracted PMD 5.4.6 into C:\pmd, so that the directory C:\pmd\pmd-bin-5.4.6 exists. Inside this directory, there are two subdirectories: bin - which contains pmd.bat and lib, which contains the necessary jars to be put onto the classpath.
So, without further configuration of your system, you should be able to run simply C:\pmd\pmd-5.4.6\bin\pmd.bat in a command shell - you should see the help screen (and not the NoClassDefFound exception).
Can you try this? Does that work for you?
If not -> reinstall PMD as your installation seems to be broken somehow.
I see, that you are calling pmd with just using pmd - this works, if you are currently in the directory C:\pmd\pmd-bin-5.4.6\bin - or if you add this directory to your PATH environment variable. Even if I do this, it works for me, regardless in which directory I'm in.
Did you configure something like the PATH environment variable?
I cannot make sense out of the error message - it claims, it can't find a class named "C:\pmd\pmd-bin-5/4/6\bin//\lib\asm-5/0/4/jar". This is not a class name, it looks like a manipulated class path - and also look at the mix of forward and backward slashes. The next part of the error is "C:\pmd\pmd-bin-5.4.6\bin..\lib\asm-5.0.4.jar" - while it might seem to be right, that there is a slash missing - it still doesn't make sense. The error message said, that it could not find the main class - but the main class is called net.soureforge.pmd.PMD.
For reference, here is the original content of pmd.bat:
#echo off
set TOPDIR=%~dp0..
set OPTS=
set MAIN_CLASS=net.sourceforge.pmd.PMD
java -classpath %TOPDIR%\lib\* %OPTS% %MAIN_CLASS% %*
Can you compare this with your pmd.bat?
I suspect, your file looks somehow different.
You can also run PMD without the batch file, e.g.:
java -classpath C:\pmd\pmd-bin-5.4.6\lib\* net.sourceforge.pmd.PMD -d path-to-source\com\vaannila\domain\User.java -f text -R java-basic
Does that work for you?
P.S.: It's true, that PMD 5.4.6 is the last one, that runs on Java 1.6. But that doesn't mean, that newer versions of PMD cannot analyze Java 1.6. code. You would just need a recent java runtime (e.g. java 10 or 11) for executing PMD and a separate JDK for building your project. PMD can analyze any version of java, it doesn't depend on the java version, it is running with.
I am trying to add the OpenCV library to the build path for my Java program from the terminal so that I can use a shell script to run the program on a bunch of image files sequentially. The .class files for my project are located in Documents/Programming/Ko/bin and the .jar file for the OpenCV library is located in Documents/Programming/opencv-2.4.10/build/bin. As suggested from a number of different questions on here, I have tried (while in the Ko/bin directory):
java -cp "/Users/jordan/Documents/Programming/opencv-2.4.10/build/bin/*:." Ko <image_name>
Which produces the following error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java2410 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1119)
at Ko.main(Ko.java:37)
So the program runs into an issue at line 37. This line is:
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
I added this line after running into issues loading the OpenCV library in Eclipse, from the advice of another question on here, although I'm not exactly sure what it does or why it is necessary. When I try commenting out this line I get a similar error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.highgui.Highgui.imread_0(Ljava/lang/String;I)J
at org.opencv.highgui.Highgui.imread_0(Native Method)
at org.opencv.highgui.Highgui.imread(Highgui.java:309)
at Ko.main(Ko.java:39)
This time at line 39, which logically is the first time I call a method from the OpenCV library.
As far as I can tell, the way I'm writing the java -cp ... command is identical to what has worked for others from the questions I've read on here. If anyone could tell me where I'm going wrong and could explain why the call to System.loadLibrary() is necessary in this particular case when in other cases of importing libraries it is not, it would be greatly appreciated.
The problem was that the command line had no knowledge of where the native libraries were on disk (i.e. the opencv2.so file). After adding an extra flag to the command and specifying the path to the native libraries, the program executed correctly:
java -D'java.library.path=<path to native libraries>' -cp '<path to Ko.class>:<path to opencv-2.4.x jar file>' Ko <path to input image file>
The first flag (-D) sets the java.library.path variable to the native libraries specific to this application so that the JRE knows where the jar file is linking to.
The second flag (-cp) sets the class path (which directories the JRE looks in for files to run). These directories are separated by colons (:) on OSX/Linux/Unix and separated by semicolons (;) on Windows.
Hope this helps anyone who runs across this problem in the future.
I've read a number of answers on classpaths, and i can't understand why this isn't working; Notice the directory listing beforehand that shows jdbc.jar is clearly in the Drivers directory, and then the java command still fails. Also, on my windows machine I can run this exact program with the same setup if i put a ; at the end of the -cp ( "./Drivers/*; ). java -version is java version "1.6.0_37".
[~/test ] $ ls -1 ./Drivers/
dbdump-0.1.0-SNAPSHOT-standalone.jar
jt400.jar
jtds.jar
[~/test ] $ java -cp "./Drivers/*" dbdump.core input.config
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:jtds:sqlserver://SERVER;instance=MSSQLSERVER;database=dbone;
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at clojure.java.jdbc$get_connection.invoke(jdbc.clj:219)
at dbdump.core$_main.invoke(core.clj:60)
at clojure.lang.AFn.applyToHelper(AFn.java:161)
at clojure.lang.AFn.applyTo(AFn.java:151)
at dbdump.core.main(Unknown Source)
[~/test ] $
java handles path same whether be it on windows or linux.
So question is if ; is working on windows why it will not work on linux.
Secondly have you tried executing the same code or program on any other linux machine.
I am presuming may be the jar is corrupt. What about pin-pointedly mentioning the jar. not just * to pick up all jars
yep what nishant mentions is the correct usage of java call. along with loading the jar we also need to call the class
"lib/*" should be enough (reference):
java -cp "Drivers/*" com.example.Classname
Class path entries can contain the basename wildcard character *,
which is considered equivalent to specifying a list of all the files
in the directory with the extension .jar or .JAR. For example, the
class path entry foo/* specifies all JAR files in the directory named
foo.
For what it is worth, this is how things are handled on windows.
I am very new to Apple scripting so please bear with me. I need to run a .jar file using applescript, the jar is not executable so I invoke the class like com.path.to.myClass. My Apple script looks like below-
display alert "You are about to start the image rename process." buttons {"OK", "Cancel"}
set theAnswer to button returned of the result
if theAnswer is "OK" then
do shell script "java -classpath ./ImageRename_JAVA-1.0.0.jar:. com.mff.image.rename.Main"
else
say "Exit"
end if
Both the applescript and the ImageRename_JAVA-1.0.0.jar are in the same directory, but when I run the script it gives me an error-
error "Exception in thread \"main\" java.lang.NoClassDefFoundError: com/mff/image/rename/Main
Caused by: java.lang.ClassNotFoundException: com.mff.image.rename.Main
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)" number 1
Am I setting the classpath wrong? If so, what is the correct way? Also, how can I add more jars to the classpath?
When I run the below command from Terminal it runs just fine.
$ java -classpath ./ImageRename_JAVA-1.0.0.jar:. com.mff.image.rename.Main
I know that it can be done in a better way using JAR Bundler but I have no control over the JAR and its developed by someone else. Is there a way that I can include all the JARs inside the application under YourApplicationName.app/Contents/MacOS/Resources/Java/ directory and use those in the class path.
I don't think you can guarantee what the working directory is in a do shell script, but you can work it out with something like this:
set scriptPath to the POSIX path of (path to me)
do shell script "SCRIPTDIR=`dirname " & scriptPath & "` ; " ¬
& "java -classpath $SCRIPTDIR/ImageRename_JAVA-1.0.0.jar:$SCRIPTDIR com.mff.image.rename.Main"
To add extra JARs to the classpath you can take advantage of a shortcut provided by the java command whereby a classpath entry ending in * includes all .jar files in the given directory.
do shell script "java -classpath " ¬
& "/Applications/Something.app/Contents/Resources/Java/\\* com.example.MyClass"
The * needs to be backslash escaped to protect it from expansion by the shell, and the backslash itself needs to be backslash-escaped when it is within an AppleScript string literal, hence the \\*.
So I made a project with packages in Eclipse.
I have a few classes all in same package com.example.parser.
In Eclipse everything works fine.
So I wanted to run one of the class from command line.
The java/javac PATHs are set correctly and work normally if no packages are there.
The file structure looks like this:
~Project/bin/com/example/parser/*.class (all class files)
~Project/src/com/example/parser/*.java (all source files)
So in command line I go to ~Project/bin/com/example/parser
To mention OS: Windows 7 Pro
The command java ClassToRun gives
Exception in thread "main" java.lang.NoClassDefFoundError: ClassToRun (wrong name:
com/example/parser/ClassToRun) at ... (stack trace)
So I googled a bit and got to conclusion this is the correct command:
java -cp .;~Project/bin ClassToRun
Now I get this:
Error: Could not find or load main class ~Project/bin
So I could not solve this... any one can help a bit?
PS: Forgot to mention the "/" should be "\" and the "~" stands for the abs path infront.
Oh yeah and I just update to jdk 1.7...
Did you type the fully-qualified class name at the command prompt?
i.e.
java -cp .;~Project\bin name.of.package.Class
.metadata is corrupted.
Steps:
Stop eclipse, delete .metadata in workspace and restart eclipse
Import Project
Run again
(Details)