I have a java program that uses dynamic reports fine in netbeans, and even works in exe and jar when I first run it. However, if I share the jar/exe (I've even sent it and re-downloaded it on my own computer), I get an error java.lang.classnotfoundexception: net.sf.jasperreports.engine.jrdatasource. However, I have double checked my jars and I do have this class. What could be the source of this problem then?
Thanks
Edit: full error:
Exception in thread "main" java.lang.NoClassDefFoundError:
net/sf/jasperreports/engine/JRDataSource
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException:
net.sf.jasperreports.engine.JRDataSource
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)
... 7 more
if you get Caused by: java.lang.ClassNotFoundException: inside a jar file then, you need to provide the classpath arguments;
example:
java -cp my.jar -jar ExternalJarFileName.jar
you can also provide all jars use below command, only need to create a directory and put all external jars in to it and use path of directory in to below command:
java -cp my.jar;C:\externalJar\* mainpackage.MainClass
Related
I have made many attempts to enter a command to run a JAR file correctly from the terminal, and I am 100% sure that all the JAR files and such are in the given paths, but I keep getting a ClassDefNotFoundException. Does it have anything to do with the way that I am entering the command?
C:\Users\ANNA\Downloads>"C:\Program Files\Java\jre1.8.0_45\bin\java.exe"
-Djava.library.path="C:\Users\ANNA\Downloads\Windows64_Libjitsi\lib\native\windows-64"
-cp
"C:\Users\ANNA\Downloads\SimplifiedConnectionProvider.jar;C:\Users\ANNA\Downloads\Windows64_Libjitsi"
Core.PublicService
Exception in thread "main" java.lang.NoClassDefFoundError: org/jitsi/service/lib
jitsi/LibJitsi
at Core.PublicService.<clinit>(PublicService.java:38)
Caused by: java.lang.ClassNotFoundException: org.jitsi.service.libjitsi.LibJitsi
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
Didn't work. Tried again:
C:\Users\ANNA\Downloads>"C:\Program Files\Java\jre1.8.0_45\bin\java.exe" -Djava.
library.path="C:\Users\ANNA\Downloads\Windows64_Libjitsi\lib\native\windows-64"
-cp C:\Users\ANNA\Downloads\SimplifiedConnectionProvider.jar;C:\Users\ANNA\Downl
oads\Windows64_Libjitsi Core.PublicService
Exception in thread "main" java.lang.NoClassDefFoundError: org/jitsi/service/lib
jitsi/LibJitsi
at Core.PublicService.<clinit>(PublicService.java:38)
Caused by: java.lang.ClassNotFoundException: org.jitsi.service.libjitsi.LibJitsi
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
Didn't work. Tried again:
C:\Users\ANNA\Downloads>"C:\Program Files\Java\jre1.8.0_45\bin\java.exe" -Djava.
library.path=C:\Users\ANNA\Downloads\Windows64_Libjitsi\lib\native\windows-64 -c
p C:\Users\ANNA\Downloads\SimplifiedConnectionProvider.jar;C:\Users\ANNA\Downloa
ds\Windows64_Libjitsi Core.PublicService
Exception in thread "main" java.lang.NoClassDefFoundError: org/jitsi/service/lib
jitsi/LibJitsi
at Core.PublicService.<clinit>(PublicService.java:38)
Caused by: java.lang.ClassNotFoundException: org.jitsi.service.libjitsi.LibJitsi
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
Note that the class "org.jitsi.service.libjitsi.LibJitsi" really is in a JAR file whose real location is in the folder "C:\Users\Anna\Downloads\Windows64_Libjitsi", which is the class path for JAR files that I am attempting to specify on the command line.
You have add the JAR to the CLASSPATH, not the folder which contains this JAR. So the -cp argument should something be like this C:\Users\ANNA\Downloads\SimplifiedConnectionProvider.jar;C:\Users\ANNA\Downloads\Windows64_Libjitsi\the_name_of_the_JAR.jar.
I used launch4j (http://launch4j.sourceforge.net/) software to create an executable of my application created in java netbeans.
I did everything as I saw in a tutorial, just that when I run the exectutavel it creates in my application, it gives this error:
The message shown below is:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/MessagingException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
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)
... 6 more
Does anyone can help me solve this problem please.
I've been searching but I can not solve. I apologize for this question but am new to java.
Thank you all.
Greetings
In your launch4j configuration file add the jars like this
<classPath mainClass="your.main.class">
<cp>path/to/javamail.jar</cp>
<cp>path/to/folder/*.jar</cp> // you can also add all the jars in a folder like this
</classPath>
It will automatically bundle them.
Hope this helps
EDIT
Just went through netbeans integration with launch4j and it seems there is no way to write your on config xml file instead it prompts you in launch4J GUI for dependencies. I searched and found this Stackoverflow Post. This is what you want. Good Luck.
Add Java mail jar to classpath.
Download it from here
http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html#javamail-1.4.7-oth-JPR
Good Morning, I have a problem when I try to execute my .jar. My program read some file from distant server by JSch.
C:\Documents and Settings\julie\Bureau>java -jar myFile.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/jcraft/jsch/SftpE xception
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source) Caused by: java.lang.ClassNotFoundException: com.jcraft.jsch.SftpException
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)
... 6 more
You have a missing classes in this package : com.jcraft.jsch.
If this package come from a third part library, then make sure that the jar contais this package inside a lib folder at the same level of your executable jar.
[Edit]
First: to add a third part jars in eclipse:
Right click on your project
Select properties -> Java Build Path -> Libraries tap.
Select Add External JARs
Navigate to your jars and press OK
Second: to generate an executable Jar file:
Right click on your project
Select Export -> Java -> Runnable JAR File.
This should work just fine.
I want to execute a Java class from a bat file and it include usage of opencsv.jar.
Below is the code:
#echo off
set path=%PATH%;C:\Program Files (x86)\Java\jdk1.6.0\bin
javac -cp opencsv.jar ArbitrageUsingThread.java
java ArbitrageUsingThread
I am getting the following error when running it. I think there is some problem with including opencsv.jar Can anyone help me with this?
Error
Exception in thread "main" java.lang.NoClassDefFoundError: au/com/bytecode/openc
sv/CSVWriter
at ArbitrageUsingThread.main(ArbitrageUsingThread.java:67)
Caused by: java.lang.ClassNotFoundException: au.com.bytecode.opencsv.CSVWriter
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
java is similar to javac for classpath.
Under linux:
java -cp opencsv.jar:. ArbitrageUsingThread
Under windows:
java -cp opencsv.jar;. ArbitrageUsingThread
after export from eclipse i have error:
C:\Program Files\Java\jre6\bin>java C:\wamp\www\JOGL\test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: C:\wamp\www\JOGL\test/jar
Caused by: java.lang.ClassNotFoundException: C:\wamp\www\JOGL\test.jar
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: C:\wamp\www\JOGL\test.jar. Program will exit.
manifest from this file:
Manifest-Version: 1.0
Sealed: true
Main-Class: windows.SimpleScene
the same error when export with Fat Jar
updated:
C:\Program Files\Java\jre6\bin>java -jar C:\wamp\www\JOGL\test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/opengl/GLEventListener
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(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)
Caused by: java.lang.ClassNotFoundException: javax.media.opengl.GLEventListener
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)
... 12 more
Could not find the main class: windows.SimpleScene. Program will exit.
To run a jar file, you run:
java -jar (name-of-jar-file)
So you should run:
java -jar C:\wamp\www\JOGL\test.jar
Make sure, when you are exporting and creating you JAR three things are set:
Export it as a "runnable jar file".
Define your launch configuration (which is your class that has the main method in it).
select "Package Required libraries into generated JAR".
The command you entered only "java file.jar" is used to run .class files, the right syntax to run jar is the "java -jar file.jar" that search your jar for the manifest and execute it.
You can also run it by adding in the class-path and the main class name
java -cp C:\wamp\www\JOGL\test.jar com.ext.Example
That's not how you run a JAR file!
Use java -jar test.jar syntax
The reason you are seeing this error is java is looking at your current working directory as the classpath. where as your JAR file is located in a different directory.
You have the following options:
1)
cd C:\wamp\www\JOGL
java -jar test.jar (as you defined the Main-Class in the manifest file, java would pick it from there).
Make sure your jar maintains the same directory structure as your package structure.
2) java -cp C:\wamp\www\JOGL\test.jar window.SimpleClass (pointing the classpath to your jar)
Note: If you have dependencies on other JARs/classes beyond this JAR file, they need to be part of the classpath (absolute/relative to current directory).