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.
Related
As a total noob in Java faced with the task of making this program work asap, unfortunately I'm stuck on a loop after consuming the basic bibliography.
The scenario is a 32-bit Linux machine (Ubuntu 16), which has installed both OpenJDK 8 and 11.
And a Java Software where tree . yields
- shiftone-jrat.jar // <-- Special jar because it's outside "lib"?
- fooModule
- foo.class
- foo.java
- ...
- barModule
- bar.class
- bar.java
- ...
- ui
- Main.class
- Main.java // <-- Special Code because it has "main()"
- ...
- lib
- jogl.jar
- vecmath.jar
- gluegen-rt.jar
- ...
- run.bat
- .classpath
The presence of run.bat files suggest that it was compiled in Windows, and after opening .classpath one can note the process was managed from the Eclipse IDE.
As far as I understand Java is very portable so there is no need to recompile the files, we only need to run it.
The only command that has worked without errors is
java -jar shiftone-jrat.jar
But after a quick Google search one can note that jrat is only a profiler.
Thus, the relevant call has to be
java ui.Main
Nevertheless, that yields noClassDefFoundError: the first thing it can complain about before termination is a package called 'vecmath' which is part of our jars in ./lib. Therefore the option cp is added to the command java, like this
java -cp "lib/*" ui.Main"
In this case the problem is that ui.Main itself is not found, so the option is passed a new argument like this:
java -cp ".:lib/*" ui.Main"
Up to this point the previous questions on the same topic here on StackOverflow are very informative. For this particular case they are not sufficient, as the previous command opens a (blank) window that reports the following error:
no gluegen-rt in java.library.path
Where java.library.path includes common root places (e.g. /usr/lib/jni, /usr/lib, /lib and /usr/java/packages/lib).
Please note that there is a .jar called gluegen-rt.jar at ./lib, the same folder that made the previous error vanish by providing a jar called vecmath.jar.
Without fully understanding what .jars are, I downloaded libgluegen2 (and perhaps also jogamp,or it is included? sorry, I don't remember) from ubuntu apt.pkgs (which is the only one I've found, i.e. the "2"), and replaced the
lib/gluegen-rt.jar file with the jar from
/usr/share/java/gluegen2-2.3.2-rt.jar.
Again, I ran
java -cp ".:lib/*" ui.Main"
And now the NoClassDefFoundError mentions gluegen/runtime/DynamicLookupHelper.
A quick (cd /usr/share/java ; grep -r "DynamicLookupHelper") shows that jogl2.jar also matches and thus I also overwrite
lib/jogl.jar file with the jar from
/usr/share/java/jogl2.jar.
Finally the NoClassDefFoundError error is thrown again, this time because there is no such thing as javax.media.opengl, which is often used in the program java's files at imports.
A quick Google search yields that Java's implementation of OpenGL had "some" changes at "some" point in time, thus the lines
javax.media.opengl should be changed to
com.jogamp.opengl to comply with our "newest" version.
I made the changes but then some other lines were broken, so finally I appear to be faced with either (1) looking for the "right" jogamp and/or gluegen or (2) continue the migration. There may be 5k lines.
Questions such as: Is there any other option I'm missing? Is the whole procedure correct? Where are old versions available? Can I leverage the xml-styled .classpath file?
Thanks
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 ;.
I currently have an open instance of the command prompt, it reads
java -cp .;jars/GUI.jar;jars/utils.jar;...(a bunch more jars in the same folder)... Test
where Test is my java program (class files already compiled).
When I hit enter, the program runs just fine. Now, I put the same exact line into a batch file for automatic runs but it gives me an error.
Batch File:
cd
java -cp .;jars/GUI.jar;jars/utils.jar;...(a bunch more jars in the same folder)... Test
pause
Error: Exception in Thread "Main" java.lang.NoClassDefFound Test (wrong name: Test)
I've been at this for hours today so at this point, this is the last thing I need to do, my brain is fried, am I missing something simple?
Or the Bash script doesn't know where the PATH to the Java installation. I'd check the environment variables as well.
Maybe you should try providing the full path of Test class: /x/y/Test
I'm not really sure of the exact rules but i guess the computer can't know which class you want to launch if Test.class is not in the current folder (current script folder? current cd/home folder?)
[UPDATE] Was just an idiot mistake. See end for solution.
I am trying to install Maven on my 2011 Macbook Pro. Unfortunately I don't know Java at all, but I am trying to muddle my way through some tools built in Java, and they imply that using Maven to install them will make things easier.
I followed the Maven install instructions here: http://maven.apache.org/download.html#Installation.
I...
1) downloaded Maven,
2) unzipped it,
3) put it in /usr/local/apache-maven-3.0.3/
4) added
export M2_HOME=/usr/local/apache-maven/apache-maven-3.0.3`
export M2=$M2_HOME/bin`
export PATH=$M2:$PATH`
to my .zshrc file
5) typed mvn --version and got back:
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/plexus/classworlds/launcher/Launcher
Caused by: java.lang.ClassNotFoundException: org.codehaus.plexus.classworlds.launcher.Launcher
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 found a couple of blog posts (http://steveswinsburg.wordpress.com/2011/03/09/java-update-broke-my-maven/) that described how to fix a similar sounding error, but those seem to be problems with upgrading Maven 2 to Maven 3. Afaik, I've never even had Maven 2. I nevertheless followed the directions in that post, and am still getting the exact same error.
Please help!
Also does anyone know a good resource for a beginner to learn how the "Java ecosystem", i.e. Maven, classpaths, etc works? I tried some "Learn Java" videos at udemy.com, but they were all about the syntax of programming in Java, whereas most of the problems I've been having have been around figuring out how to make existing Java apps run in the first place, or how to follow basic jargon-filled documentation.
[SOLUTION]
Summary...idiot mistake.
While following the Maven installation directions, I cut-n-pasted the line "In a command terminal, add the M2_HOME environment variable, e.g. export M2_HOME=/usr/local/apache-maven/apache-maven-3.0.3", not realizing that I had actually installed my unzipped file in /usr/local/apache-maven-3.0.3.
Once again, scumbag brain "saves time" by refusing to automatically read apparently redundant information, so I didn't notice that apache's example expected I had nested the install inside a second folder.
Fixing the .zshrc line to read `export M2_HOME=/usr/local/apache-maven-3.0.3' fixed the problem.
Thanks ivantod for pointing out my error.
Mac OS X Lion ships with maven already installed on it # /usr/share/maven.
You only have to type java on terminal to install java and export JAVA_HOME in ~/.profile to /System/Library/Frameworks/JavaVM.framework/Home. Optionally you can set other parameters like MAVEN_OPTS and JAVA_OPTS.
I am developing a program that calls R functions from Java using JRI/rJava. I was coding the program in NetBeans on another machine, which was working fine (i.e. able to run the code). I have since then moved to another machine and have been running into problems.
The exact error message I am seeing is this:
Cannot find JRI native library!
Please make sure that the JRI native library is in a directory listed in java.library.path.
java.lang.UnsatisfiedLinkError: E:\R\R-2.13.1\library\rJava\jri\jri.dll: The specified path is invalid
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1807)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1732)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at org.rosuda.JRI.Rengine.<clinit>(Rengine.java:19)
at com.rjava.test.rtest.main(rtest.java:64)
Java Result: 1
I have read the FAQs for JRI/rJava, and have been scouring the internet for fixes, but have made no progress. Here is what I have done so far:
Created an environment variable called R_HOME: "E:\R\R-2.13.1"
Added "%R_HOME%\bin\x64" to the PATH environment variable
Added "%R_HOME%\library\rJava\JRI" to the PATH environment variable (this is where jri.dll is located)
Set the required jar files as compile time libraries (JRI.jar, JRIEngine.jar, REngine.jar) in NetBeans
set the following VM options in NetBeans: : -Djava.library.path=E:\R\R-2.13.1\library\rJava\jri (This is where jri.dll is located)
I have restarted my computer to make sure that the changes stick.
To make sure I configured things correctly, I ran the following in the command line:
java -cp E:\R\R-2.13.1\library\rJava\jri\JRI.jar;E:\R\R-2.13.1\library\rJava\jri\examples rtest
And the example java files ran fine. I'm beginning to think my new machine just hates me.
The message indicates that it the path E:\R\R-2.13.1\library\rJava\jri\jri.dll is invalid. Are you sure that path exists? Also, is E a mapped drive that is mapped to a path that has spaces in it? I'm not sure if the spaces are the issue, but it eliminates one issue. I would try just putting the dll in C:\ or somewhere very simple and seeing if it can find it there as a simple test.
Also verify that the -Djava.library.path is being passed as you think it is (you can check that with visualvm or jconsole).
You could try this:
-Djava.library.path=E:\R\R-2.13.1\library\rJava\jri -cp E:\R\R-2.13.1\library
\rJava\jri;E:\R\R-2.13.1\library\rJava\jri\JRI.jar;E:\R\R-2.13.1
\library\rJava\jri\examples
The reason I say this is that, perhaps the .dll also needs to be in the classpath as well as the library path in order for the classloader to load it? Its probably not true, but worth trying. Also is "rJava" correct? Other than that, it looks to me like your doing it right.
To locate JRI installed with rJava, use system.file("jri",package="rJava") in R.
set that path to your path (environment variables in windows),
restart your netbeans. and try to run your program again