Problem accessing one jar file from another - java

I've written a Java program that uses classes from the Apache PDFBox jar application. I have my compiled classes and the PDFBox jar file in on directory. I can run this successfully:
java -cp .;pdfbox-app.jar Athena NPCGenerator -pdf
However, when I jar up my own program and try to run it in the same place in a similar fashion, this fails:
java -cp .;pdfbox-app.jar -jar Athena.jar NPCGenerator -pdf
Error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/pdfbox/pdm
odel/PDDocument
at CharacterPDF.writePDF(CharacterPDF.java:49)
at NPCGenerator.printToPDF(NPCGenerator.java:302)
at NPCGenerator.makeAllNPCs(NPCGenerator.java:278)
at NPCGenerator.main(NPCGenerator.java:316)
at Athena.runApp(Athena.java:88)
at Athena.main(Athena.java:104)
Caused by: java.lang.ClassNotFoundException: org.apache.pdfbox.pdmodel.PDDocumen
t
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)
... 6 more
What do I need to do to fix this?

If you use the -jar flag to java it expects a executable jar. It will then ignore the classpath on the command line and read the classpath from the jars manifest.
You can either add pdfbox-app.jar to the manifest in Athena.jar together with the main-class attribute. You can even specify a relative or absolute path to the jar in the manifest.
Main-Class: Athena
Class-Path: pdfbox-app.jar
Or you can not use -jar and add Athena.jar to your classpath via the command line. In this case you need to also specify the main class on the command line as java will not read the manifest in this case.
java -cp .;pdfbox-app.jar;Athena.jar Athena NPCGenerator -pdf

Related

Why occurs a java.lang.NoClassDefFoundError when I use args4j in a batch built by mvn:assembly

For various experimentations, I take care of a java project in github.
After the Maven build, the program runs with a script bat.
Now I opened a branch because I would use the library args4j to parsing the arguments.
The build works fine, the jars exist in the directory lib, but when I run I have this stacktrace of Exception
Exception in thread "main" java.lang.NoClassDefFoundError:
org/kohsuke/args4j/CmdLineException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2625)
at java.lang.Class.getMethod0(Class.java:2866)
at java.lang.Class.getMethod(Class.java:1676)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException:
org.kohsuke.args4j.CmdLineException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 6 more
in bat I configured the classpath so that the args4j jar in in lib: this are the instructions of bat script
SET JAVA_DIR=C:\Program Files\Java\jdk1.7.0_80\bin\
>CUT
"%JAVA_DIR%\java" -jar ".\lib\buildCSS-1.0.jar" -cp ".\lib\" -conf "./conf/environment.properties"
I don't understand the deal of java.lang.NoClassDefFoundError. The jar are present and linked by -cp option
Do you have any idea (and solution), please?
You cannot combine -jar and -cp arguments on the command line. If the java command sees -jar it treats everything after the jarfile name as application arguments, AND it ignores any earlier classpath arguments.
You have two choices:
Use -cp, include the main JAR in the classpath, and put the full class name for the main class on the command line.
Use -jar, and add a "Class-Path" attribute to the main JAR's manifest file listing all of the dependencies.
References:
The java command page - explains -jar versus -cp
The JAR file specification - explains the "Class-Path" attribute
Note: since you are building the JAR file using Maven, there are other options; for example
Use the "Shade" plugin to create an executable "uber-jar" containing all of the dependencies in a single JAR.

Java 3d error when running application from .jar

Hello when i am running my application from Eclipse it runs perfectly but when i export it as a runnable jar, it doesn't run.
When i try running it from cmd it gives me one of 2 errors the first errror is after i just export it with the "copy required libraries into a sub-folder":
Exception in thread "main" java.lang.UnsatisfiedLinkError: no j3dcore-ogl in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at javax.media.j3d.NativePipeline$1.run(NativePipeline.java:189)
at java.security.AccessController.doPrivileged(Native Method)
at javax.media.j3d.NativePipeline.loadLibrary(NativePipeline.java:180)
at javax.media.j3d.NativePipeline.loadLibraries(NativePipeline.java:137)
at javax.media.j3d.MasterControl.loadLibraries(MasterControl.java:948)
at javax.media.j3d.VirtualUniverse.<clinit>(VirtualUniverse.java:280)
at javax.media.j3d.Canvas3D.<clinit>(Canvas3D.java:3862)
at Main.Game.<init>(Game.java:39)
at Main.Main.main(Main.java:6)
when i change the sub-folder's name from IslandDomination_lib to just lib it gives me another error:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/j3d/Canvas3D
at Main.Main.main(Main.java:6)
Caused by: java.lang.ClassNotFoundException: javax.media.j3d.Canvas3D
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 have tried running it several ways:
1.
java.exe -jar IslandDomination.jar
2.
java.exe -cp "lib/j3dcore.jar;lib/j3dutils.jar;lib/vecmath.jar" -jar IslandDomination.jar
3. using another java program to run it for me:
package main;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException{
ProcessBuilder pb = new ProcessBuilder(System.getProperty("java.home")+"\\bin\\java.exe", "-cp" , "\"lib/j3dcore.jar;lib/j3dutils.jar;lib/vecmath.jar\"" , "-jar" , "IslandDomination.jar");
pb.directory(new File("./"));
Process p = pb.start();
p.waitFor();
}
}
The internal structure of my jar consists of 4 folders:
J3DBool
Main
Maths
META-INF
The folder with the required jars is in the same folder as the main jar
You use a completely obsolete version of Java3D, which is very difficult to bundle in any software as you have to set the Java library path somehow and you have to bundle both the Java libraries and the native libraries. Rather rebuild your project with Java3D 1.6.0 pre 12, it's a lot easier to use as this version relies on JOGL 2 which uses automatic native library loading and everything is packaged as JARs, there is no longer any need of modifying the library path, just take care of the classpath.
Please follow my tutorial, especially the very last section with (a lot) more information.
Finally, the obsolete version that you use isn't guaranteed to work on any recent operating systems. If it works, you'll be lucky; if it doesn't, nobody will fix it.
I think the required library file (jar file) not found in the classpath.
you need to set Library file in your class path.
step to set class path:-
MyComputer->Properties->Advanced system setting->Environment variables
find "classpath" either in user variables area or in system variables if found edit that and put all the jar file link. if not found then create new(click on new) and put all the jar link there and try to run your application
I found out how to fix my problem and it is done by simply putting the needed dll files next to the executable jar file

NoClassDefFoundError, in the same directory why the program can't find the jar

I write a Java program that read text from excel.So I import some jar like this: poi-3.10-beta2-20130904.jar, poi-ooxml-3.10-beta2-20130904.jar, etc., I can run the program correctly in eclipse.But when I package this program with maven to the directory(C:\workspace2\change\bin),
Then I run this program in command like this :
C:\workspace2\change\bin>java GenerateVar
it occurs this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Cell
at GenerateVar.execute(GenerateVar.java:59)
at GenerateVar.main(GenerateVar.java:25)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Cell
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)
... 2 more
You need to specify classpath with all other jars/classes you use in your program.
E.g. Setting multiple jars in java classpath
When you run it from eclipse in the console there's a command that could be used to run the project, you could copy/paste it from the console to your command line.
In the command java GenerateVar you didn't set -cp option. With this option the command line will look like
java -cp poi-3.10-beta2-20130904.jar poi-ooxml-3.10-beta2-20130904.jar ... GenerateVar
Assumed the libraries are in the current dir.
How to use maven plugins to add dependency to your project and build jar you can find here.
See also this answer if you want to modify manifest.mf manually.
over, I change the way do what I want to do. I use the fat jar which is the eclipse plugin to package the program

JAVA: Unable to run a jar file

I'm new to JAVA and trying unable to figure out what's wrong with the project I created.
I created a JAVA project in eclipse and exported a jar (not runnable jar).
I unchecked .project and .classpath file options while exporting. Now I'm trying to run that jar and getting NoClassDefFoundError:
I'm pasting the command and the errors below:
C:\Users\Anjali>"C:\Program Files\Java\jre7\bin\java.exe" -classpath D:\Web\Hbas
eGateway\bin;C:\Users\Anjali\Downloads\Hbase\hbase-0.94.5-security.jar;C:\Users\
Anjali\Downloads\Hbase\lib\hadoop-core-1.0.4.jar;C:\Users\Anjali\Downloads\Hbase
\py4j0.7.jar -jar D:\Web\HbaseGateway\bin\HBaseGateway.jar
Exception in thread "main" java.lang.NoClassDefFoundError: py4j/GatewayServer
at hbase.gateway.HBaseGatewayEntryPoint.main(HBaseGatewayEntryPoint.java
:22)
Caused by: java.lang.ClassNotFoundException: py4j.GatewayServer
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)
... 1 more
Now, interesting thing is I'm able to run the main class using the following command.
In the below command I removed the -jar switch and passed the main class to java.exe.
C:\Users\Anjali>"C:\Program Files\Java\jre7\bin\java.exe" -classpath D:\Web\Hbas
eGateway\bin;C:\Users\Anjali\Downloads\Hbase\hbase-0.94.5-security.jar;C:\Users\
Anjali\Downloads\Hbase\lib\hadoop-core-1.0.4.jar;C:\Users\Anjali\Downloads\Hbase
\py4j0.7.jar hbase.gateway.HBaseGatewayEntryPoint
Gateway Server Started
Why NoClassDefFoundError is being thrown? Am I missing something here?
For -jar option to work, you must add Main-class key in manifest.
Refer the java command documentation http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/java.html
-jar option
Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name.
In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point.
When you use the -jar option, the classpath is read from the MANIFEST.mf file as well. From the docs for the -jar option:
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
This means that you have to add all your dependencies in a Class-Path section of the manifest file.

problem related to MANIFEST.MF in jar

I have created jar in folder /usr/local/bin/niidle.jar.
And I have cerated MANIFEST.MF in jar as follows:
Manifest-Version: 1.0
Main-Class: com.ensarm.niidle.web.scraper.NiidleScrapeManager
Class-Path: hector-0.6.0-17.jar
And my 'hector-0.6.0-17.jar' file is present in following folder:
/Projects/EnwelibDatedOct13/Niidle/lib/hector-0.6.0-17.jar
And I have added this 'hector-0.6.0-17.jar' in niidle.jar folder as follows:
niidle.jar/lib/hector-0.6.0-17.jar
And when I run this using command:
java -jar /usr/local/bin/niidle.jar arguments....
then I am getting error message as follows:
Exception in thread "main" java.lang.NoClassDefFoundError: me/prettyprint/hector/api/Serializer
at com.ensarm.niidle.web.scraper.NiidleScrapeManager.main(NiidleScrapeManager.java:21)
Caused by: java.lang.ClassNotFoundException: me.prettyprint.hector.api.Serializer
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 1 more
And When I run again using different command as follows:
java -cp /Projects/EnwelibDatedOct13/Niidle/lib/hector-0.6.0-17.jar -jar /usr/local/bin/niidle.jar arguments.....
---------------Then also I am getting the same error message:--
Exception in thread "main" java.lang.NoClassDefFoundError: me/prettyprint/hector/api/Serializer
at com.ensarm.niidle.web.scraper.NiidleScrapeManager.main(NiidleScrapeManager.java:21)
Caused by: java.lang.ClassNotFoundException: me.prettyprint.hector.api.Serializer
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 1 more-------------
so tell me what is solution for that.or any modification in MANIFEST.MF in jar
java -cp /Projects/EnwelibDatedOct13/Niidle/lib/hector-0.6.0-17.jar -jar /usr/local/bin/niidle.jar arguments.....
You can't use the -cp switch together with the -jar switch.
From http://mindprod.com/jgloss/classpath.html:
If you use the -jar option, java.exe ignores the classpath. It will only look in that jar!! Try using the manifest Class-Path instead to get Java to look in auxiliary jars
And I have added this 'hector-0.6.0-17.jar' in niidle.jar folder as follows:
To my knowledge, you can't nest .jar files this way.
Since you have Class-Path: hector-0.6.0-17.jar you need to have the needle.jar and the hector-0.6.0-17.jar side by side and run niidle.jar from their common directory (using the -jar switch if you like).
Line niidle.jar/lib/hector-0.6.0-17.jar is confusing. Is niidle.jar directory or file?
I'd suggest you to post your full manifest, directory structure, command line you are using and current working directory.
But generally you should write in your manifest:
Class-Path: hector-0.6.0-17.jar
then put all jars in one directory. Let's call it foo.
Now open command prompt, go to foo and run
java -jar niidle.jar args...
And I believe that everything will work. Then if you wish play with location of your jars.
For setting up "jars within jars" take a look at this question:
Classpath including JAR within a JAR

Categories

Resources