How can i solved execute .jar? - java

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.

Related

Can not refer to class packed in Jar1 that zipped in Jar2 while running Jar2

I'm getting this weird issue when my colleague runs my Jar in JRE of same version as my development environment (Java1.7 + Eclipse Indiago).
Add a Jar to build path and refer to some Classes in this jar;
Compile, run the project as Java application successfully in Eclipse;
Export the project as Runnable Jar with option - 'Package required libraries into generated Jar'.
Run the exported Jar in command line 'java -jar xxx.jar' in my computer and get expected result.
Run the same Jar in another computer with same JRE but get something error like 'ClassNotFound' telling that the class referred from inner Jar can not be found, as shown below:
Exception in thread "main" java.lang.NoClassDefFoundError: com/test/commo
n/communication/BaseNotificationEvent
at java.lang.ClassLoader.defineClass1(Native Method)
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$100(Unknown Source)
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 java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:56)
Caused by: java.lang.ClassNotFoundException: com.test.common.communicatio
n.BaseNotificationEvent
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 java.lang.ClassLoader.loadClass(Unknown Source)
... 14 more
The solution so far is to export runnable jar with option 'copy required libraries into sub-folder next to generated Jar', and this works both in my and another computer.
But I am not take this as ultimate solution for I have to transfer multiple files rather a single Jar while releasing my program...which seems not elegant ^_^

Dynamic reports classnotfoundexception

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

Exporting Runnable JAR in eclipse with User Libraries included

I am trying to create a runnable JAR file from a project of which I'm importing org.apache.commons.io. I tried exporting and checked the box to include all dependant libraries but I keep getting this output:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/io
/filefilter/TrueFileFilter
at tvSort.Main.main(Main.java:36)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.filefilter.Tr
ueFileFilter
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
Any help would be appreciated, thank you.
Did you try setting up classpath in the Manifest?
e.g. Class-Path: commons-io-xyzversion.jar
If you can please try to add this manifest entry manually by saving this in a text file and adding it via the jar tool.
Hopefully eclipse has already done this,so did you run the jar with dependencies(jars) in the same folder/path?

Failed to load Main-Class manifest attribute from

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).

What's wrong with this .jar file?

I'm trying to turn my JoGL project into a jar file. What am I doing wrong?
Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/opengl/GL
EventListener
at java.lang.ClassLoader.defineClass1(Native Method)
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)
at java.lang.ClassLoader.loadClassInternal(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)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 12 more
Could not find the main class: chartest. Program will exit.
Contents of the JAR file:
META-INF\MANIFEST.MF
gluegen-rt.jar
jogl.jar
chartest$1.class
chartest.class
gluegen-rt.dll
jogl.dll
jogl_awt.dll
jogl_cg.dll
test.png
You can check your classpath. It should include atleast
The jar file with classes you have written
Supporting jars with classes like javax.media.opengl (jogl.jar?)
Try placing all these jars in your classpath and run it.
is Jogle jar file in your classpath ?
I see you have included the jars themselves in your jar... this unfortunately wil not work out of the box.
Either get the jars out then place in your classpath (that can be part of your manifest also) or write your own classloader...
http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html
This is why it worked for William and not for you... he got the jogl from somewhere else and did not use the one within your jar.
Switch to Netbeans to build your jar file. I find using other libraries inside of my projects including dll files its best to use something like Netbeans or just package it with maven.
mvn clean package
You need to be well versed in xml though if you want to use maven to package all of your jar files.

Categories

Resources