ant runnable jar not working - java

I created a runnable jar using ant but it is not running. when I run the jar I am getting following error
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/teamdev/jxbrowser/events/NavigationListener
at com.MainClass$2.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: java.lang.ClassNotFoundException: com.teamdev.jxbrowser.events.NavigationListener
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 15 more
but when I created jar using eclipse that jar worked fine. Menifest of jar I created with ant is as below
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_37-b06-434-11M3909 (Apple Inc.)
Main-Class: com.MainClass
Class-Path: ./ commons-cli.jar commons-codec-1.2.jar commons-httpclien
t-3.0.1.jar commons-logging-1.1.1.jar ffmpeg-java.jar fmj.jar jdom.ja
r logback-classic.jar logback-core.jar lti-civil-no_s_w_t.jar mail.ja
r mp3spi1.9.4.jar slf4j-api.jar tritonus_share.jar vorbisspi1.0.2.jar
xuggle-xuggler.jar comfyj-2.9.jar engine-gecko.jar engine-ie.jar eng
ine-webkit.jar jniwrap-3.8.4 jxbrowser-3.3.jar jxbrowserdemo.jar log4
j-1.2.15.jar MozillaInterfaces.jar runtime.jar slf4j-api-1.5.8.jar sl
f4j-log4j12-1.5.8.jar winpack-3.8.3.jar xulrunner-mac.jar
and menifest of jar I created with eclipse is as below
Manifest-Version: 1.0
Rsrc-Class-Path: ./ jniwrap-3.8.4.jar lti-civil-no_s_w_t.jar commons-h
ttpclient-3.0.1.jar slf4j-log4j12-1.5.8.jar vorbisspi1.0.2.jar slf4j-
api.jar ffmpeg-java.jar fmj.jar commons-codec-1.2.jar engine-ie.jar x
ulrunner-mac.jar commons-logging-1.1.1.jar winpack-3.8.3.jar mp3spi1.
9.4.jar tritonus_share.jar commons-cli.jar jdom.jar MozillaInterfaces
.jar comfyj-2.9.jar jxbrowser-3.3.jar runtime.jar logback-classic.jar
engine-gecko.jar mail.jar engine-webkit.jar log4j-1.2.15.jar jxbrows
erdemo.jar slf4j-api-1.5.8.jar logback-core.jar xuggle-xuggler.jar
Class-Path: .
Rsrc-Main-Class: com.MainClass
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
both are same and all dependencies are also inside the created jar but still the jar create with ant is not working.
I know you would recommend me to create jar with eclipse but I want to obfuscate my code that's why I am using ant to create runnable jar.

I guess that your jar file generated with Ant does not have jar-in-jar-loader, that's why it is not able to find classes inside embedded jars.
When you generate JAR with Eclipse you can Save Ant script, then jar-in-jar-loader.zip file would be added to project. Then use generated Ant file to create your JAR. This approach works for me.
Your Ant script should look like this:
<jar destfile="C:\Users\\workspace\Your.jar">
<manifest>
<attribute name="Main-Class" value="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader"/>
<attribute name="Rsrc-Main-Class" value="org.mypackage.MainClass"/>
<attribute name="Class-Path" value="."/>
<attribute name="Rsrc-Class-Path" value="./ libA.jar "/>
</manifest>
<zipfileset src="jar-in-jar-loader.zip"/>
<fileset dir="${ProjectPath}/bin"/>
<zipfileset dir="${ProjectPath}\lib" includes="libA.jar"/>
</jar>
First zipfileset would include jar-in-jar-loader.zip
fileset would add all your classes
Second zipfileset would add libA.jar as embedded jar and same should be mentioned in Rsrc-Class-Path

With your Ant-generated Manifest, it is looking for the library jar files outside of the main jar file, the Eclipse one uses a special Main-Class and class loader to get to the bundled dependencies.
You probably want to use the OneJar ant task (or something similar) to achieve something comparable to what Eclipse does.

Related

Java Exceptions error when trying to execute .jar on Debian Jessie

A teacher of mine created a program on java around 2005, he has always used MacOS. He sent the .jar to me and a classmate, both linux (me Debian 8 and him Ubuntu) and we can't execute the program because it returns a lot of exceptions.
At first I thought it could have been because the program was too old but my teacher can run it in his 1 year old updated mac perfectly fine.
Checked and Debian comes with OpenJDK as default. Havent installed anything, is there anything I should be installing for it to run?
PS: if I double click the .jar I can see the folders.
The exceptions are:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jfree/chart/ChartPanel
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:803)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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)
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.jfree.chart.ChartPanel
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)
... 18 more
I don't know anything about java so please if you can try to be as basic as you can. I am also new to Debian, so I really don't know if there's a package missing from the default installation.
Edit:
The MANIFEST.MF file is
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_24-b07-334-10M3326 (Apple Inc.)
Class-Path: lib/Jama-1.0.2-ed.jar lib/jfreechart-1.0.6.jar lib/ant.jar
lib/jcommon-1.0.10.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: com.cmcweb.electronicstructure.CompleteDOSPlotter
You have to download JFreeChart.jar libary (http://www.java2s.com/Code/Jar/j/Downloadjfreechartjar.htm) because your teacher didn't include external classes in his jar file.
Put JFreeChart.jar into /your_linux_path/jre/lib/ext folder and after that it should works.
P.S. The same you have to do with Jama.jar and jcommon.jar
Make sure you have all jars with the version from the manifest file downloaded in a lib directory. Lib directory should be placed in the same directory as your jar, the names of dependency jars should be exactly the same as these from classpath in manifest.
So in your case, it may not work with the newest JFreeChart version. You need to find and download version 1.0.6

Can't Export runnable jar from eclipse

i'm trying to export a runnable jar from eclipse, but this is the result:
Exception in thread "main" java.lang.NoClassDefFoundError: neg1
Caused by: java.lang.ClassNotFoundException: neg1
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)
This is the manifest:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ itextpdf-5.4.3.jar itext-pdfa-5.4.3.jar itext-xtra
-5.4.3.jar
Class-Path: .
Rsrc-Main-Class: Negozio.Start
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
And this is how the files are packed into the jar:
http://i.stack.imgur.com/uXEaQ.png
Where Operazioni and Negozio are the my packages and the itext are the external library
Did you forget to include an external jar file?
You must have external jars that you did not include when you exported or were not put in your class path. Right click on your project Properties->Java Build Path and then ensure the "Libraries" tab is selected. Ensure the jars are added here.

Exception when trying to save images

When starting my Java application, I get exceptions when trying to save images. In Eclipse, however, everything works fine. The application is built using fatjar and the necessary libraries (jar_imageio.jar and ij.jar) have been selected for export as well.
I tried using ImageIO and ImageJ:
a.) ImageIO:
ImageIO.write(image, "jpg", new File(f));
Exception in thread "main" sun.misc.ServiceConfigurationError:
javax.imageio.spi.ImageWriterSpi:
Provider com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi
could not be instantiated: java.lang.IllegalArgumentException: vendorName == null!
at sun.misc.Service.fail(Unknown Source)
at sun.misc.Service.access$200(Unknown Source)
at sun.misc.Service$LazyIterator.next(Unknown Source)
at javax.imageio.spi.IIORegistry.registerApplicationClasspathSpis(Unknown Source)
at javax.imageio.spi.IIORegistry.<init>(Unknown Source)
at javax.imageio.spi.IIORegistry.getDefaultInstance(Unknown Source)
at javax.imageio.ImageIO.<clinit>(Unknown Source)
b.) ImageJ:
IJ.saveAs(image, "jpg", f);
java.lang.NoClassDefFoundError: Could not initialize class javax.imageio.ImageIO
at ij.plugin.JpegWriter.saveAsJpeg(JpegWriter.java:49)
at ij.plugin.JpegWriter.save(JpegWriter.java:28)
at ij.io.FileSaver.saveAsJpeg(FileSaver.java:340)
at ij.io.FileSaver.saveAsJpeg(FileSaver.java:332)
at ij.plugin.filter.Writer.run(Writer.java:24)
at ij.plugin.filter.PlugInFilterRunner.processOneImage(PlugInFilterRunner.java:256)
at ij.plugin.filter.PlugInFilterRunner.<init>(PlugInFilterRunner.java:105)
at ij.IJ.runPlugIn(IJ.java:158)
at ij.Executer.runCommand(Executer.java:127)
at ij.Executer.run(Executer.java:64)
at ij.IJ.run(IJ.java:249)
at ij.IJ.run(IJ.java:296)
at ij.IJ.saveAs(IJ.java:1579)
As #Victor says I think you should look at
Exception in thread "main" sun.misc.ServiceConfigurationError:
javax.imageio.spi.ImageWriterSpi:
Provider com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi
could not be instantiated: java.lang.IllegalArgumentException: vendorName == null!
I had this issue just yesterday and it was tricky. There are similar questions here. I found if I included jai_imageio in the jar and did not modify the manifest file to include the contents of the JAI manifest file or combine the files in the services folder of META-INF in your build then I had a number of errors similar to yours. My application did work though without JAI included since JAI was installed locally I opted to build it with JAI included for the time being.
Opening you jar you will find a directory called META-INF. In there is the file MANIFEST.MF. I use Maven to include the JAI things in the Manifest file so it looks like
Manifest-Version: 1.0
Implementation-Title: com.sun.media.imageio
Implementation-Version: 1.0_01
Built-By: myName
Specification-Vendor: Sun Microsystems, Inc.
Created-By: Apache Maven
Implementation-Vendor: Sun Microsystems, Inc.
Build-Jdk: 1.6.0_43
Specification-Title: Java Advanced Imaging Image I/O Tools
Specification-Version: 1.0-mr
Extension-Name: com.sun.media.imageio
Main-Class: myMain
Archiver-Version: Plexus Archiver
You should have your name and your main class substituted in there. You could just modify this file and jar it up yourself on the command line if you don't use Maven (or Ant) to get it working. I had the extra issue where some of my included jars were overwritting files in the services folder of META-INF. Instead I merged these files using Maven's Shade plugin.
add this lines into build.xml (solved for me)
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
<attribute name="Built-By" value="${user.name}" />
<attribute name="Built-Date" value="${TODAY}" />
<attribute name="Implementation-Title" value="MyApp" />
<attribute name="Implementation-Vendor" value="MyCompany" />
<attribute name="Implementation-Version" value="${version.num}-b${build.number}"/>
</manifest>

executing jar file: Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext

I have created a maven project. After I type: mvn clean package everything works fine and I see my jar file inside target. But if I click the file I get the follwing error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/c
ontext/ApplicationContext
Caused by: java.lang.ClassNotFoundException: org.springframework.context.Applica
tionContext
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: comt.test.Test. Program will exit
I can run the project under Eclipse and works ok, but the jar file doesnt work.
This is the content of Manifest:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Build-Jdk: 1.6.0_23
Main-Class: com.stlpo.App
Class-Path: commons-httpclient-3.1.jar commons-codec-1.2.jar h2-1.2.138.jar
commons-lang-2.5.jar spring-core-3.0.3.RELEASE.jar spring-asm-3.0.3.RELEASE.jar co
mmons-logging-1.1.1.jar spring-context-3.0.3.RELEASE.jar spring-beans
-3.0.3.RELEASE.jar spring-expression-3.0.3.RELEASE.jar spring-orm-3.0
.3.RELEASE.jar spring-tx-3.0.3.RELEASE.jar spring-aop-3.0.3.RELEASE.j
ar aopalliance-1.0.jar spring-jdbc-3.0.3.RELEASE.jar spring-test-3.0.
3.RELEASE.jar cglib-nodep-2.2.jar logback-core-0.9.24.jar logback-cla
ssic-0.9.24.jar slf4j-api-1.6.0.jar hibernate-annotations-3.5.4-Final
.jar hibernate-core-3.5.4-Final.jar antlr-2.7.6.jar commons-collectio
ns-3.1.jar jta-1.1.jar hibernate-commons-annotations-3.2.0.Final.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar hibernate-entitymanager-3.5.4-F
inal.jar cglib-2.2.jar asm-3.1.jar javassist-3.9.0.GA.jar opencsv-2.1
.jar jfreechart-1.0.13.jar jcommon-1.0.16.jar commons-beanutils-1.8.3
.jar beansbinding-1.2.1.jar poi-3.6.jar log4j-1.2.13.jar
poi-ooxml-3.6.jar poi-ooxml-schemas-3.6.jar xmlbeans-2.3.0.jar stax-
api-1.0.1.jar geronimo-stax-api_1.0_spec-1.0.jar dom4j-1.6.1.jar xml-
apis-1.0.b2.jar commons-dbcp-1.4.jar commons-pool-1.5.4.jar TableLayo
ut-20050920.jar
Thanks in advance for your help.
When you build a JAR, it only has your classes in it. It doesn't contain all the other libraries that your project depends on. (Doing that is possible, but it's not common and mostly inadvisable.) To run a class in your JAR that depends on Spring, you have to be sure Spring is on the classpath, either by passing a classpath to the java executable (doesn't work if using the -jar argument) or by putting a "Class-Path" attribute in the JAR's manifest file. Of course, it's not just Spring. You'll have to include a classpath entry for every jar your project depends on.
see another post regarding the same problem
you need to create a classPath (the eclipse is dong it automatically for you, and this is why you have no problem ruining the project from the eclipse) for the dependencies (in your case the spring)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
<addClasspath>true</addClasspath>
<!-- use class path prefix if your dependencies are out side the jar)
<classpathPrefix>lib/</classpathPrefix> -->
</manifest>
<manifestEntries>
<Class-Path>conf/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>

is it possible to load jars-in-jars in JWS?

I have found MANY threads on packing all dependencies along with the project into one jar package, and it seems like there are many different ways to achieve this (oneJar, FatJar, Ant-build...)
So cooking up my own recipe, I have (after quite some effort) managed to package the project I am working on. In this one jar file, there is the code for the project plus all the jars that the project depends on which are loaded with jar-in-jar-loader that comes with eclipse. The resultant works fine on a number of different platforms, when it's ran through the terminal invoked via java -jar myjarfile.
Peachy, you might say, here's the problem though; when I sign my jar and try to run it via javaws (which is the ultimate goal here) I get an exception which I have decrypted to mean that libraries (in the case below org.apache.commons.lang.SystemUtils) are unaccessible.
So here's my question; is it not possible to load jars in jars when the applications is deployed for Java Web Start? If it is possible, what am I doing wrong? If not, what's the best alternative?
Thanks!
Below is the JNLP file along with the stackTrace I get when I run javaws myJNLPfile
<?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+"
codebase="file:///home/ukirik/workspace/myproject/dist"
href="project.jnlp">
<information>
<!-- Project info -->
</information>
<security>
<all-permissions />
</security>
<resources os="Mac\ OS\ X">
<j2se version="1.6+" java-vm-args="-XstartOnFirstThread"/>
</resources>
<resources>
<jar href="myjar-jws.jar" />
</resources>
<application-desc main-class="org.gvt.RuntimeMain"/>
</jnlp>
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.javaws.Launcher.executeApplication(Launcher.java:1799)
at com.sun.javaws.Launcher.executeMainClass(Launcher.java:1745)
at com.sun.javaws.Launcher.doLaunchApp(Launcher.java:1507)
at com.sun.javaws.Launcher.run(Launcher.java:129)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/lang/SystemUtils
at org.gvt.RuntimeMain.loadSwtJar(RuntimeMain.java:27)
at org.gvt.RuntimeMain.main(RuntimeMain.java:13)
... 9 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.SystemUtils
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 com.sun.jnlp.JNLPClassLoader.findClass(JNLPClassLoader.java:332)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 11 more
If you are using the Eclipse jar-in-jar loader I think that you may need this in the jnlp file
<application-desc main-class="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader"/>
That's assuming that your manifest is looking like this...
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.1
Created-By: 20.1-b02 (Sun Microsystems Inc.)
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
Rsrc-Main-Class: yourapp.mainclass
Rsrc-Class-Path: ./ swt.jar velocity-1.7-dep.jar
Class-Path: .
It seems like the problem is with the classloader.
You may want to use a custom classloader in JWS, as described here:
http://lopica.sourceforge.net/faq.html#customcl
Depending on exactly what you need to do, in terms of packaging, there are a couple of options.
The problem you're running into is that you're passing org.gvt.RuntimeMain as your main class for your jar in your JNPL file, but that's a one-jar jar. Because it's a one-jar jar, you need to provide class com.simontuffs.onejar.Boot instead, as
<application-desc main-class="com.simontuffs.onejar.Boot"/>
The reason for it, is that the one-jar plugin will generate that class, which will make use of a classloader that understands jars within jars, and then invoke your org.gvt.RuntimeMain class (it figures it out through looking at the manifests's One-Jar-Main-Class: header).

Categories

Resources