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>
Related
My situation is similar to this but it is a newer version of bouncycastle so I am not sure it is the same situation and it is four years later without an accepted answer:
Bouncy castle no such method error
I am using bc with eclipse milo with an ant build: my dependencies are:
bcpkix-jdk15on-1.61.jar
bcprov-jdk15on-1.61.jar
When I launch our app in eclipse we are able to connect with the UA Expert test client securely without any issues. However when connecting with UA Expert to the milo opc-ua server on our deployed application on a SBC I am seeing this error:
Mar 05, 2020 6:54:48 PM org.eclipse.milo.opcua.stack.core.util.ExecutionQueue$Task run
WARNING: Uncaught Throwable during execution.
java.lang.NoSuchMethodError: org.bouncycastle.util.Arrays.areAllZeroes([BII)Z
at org.eclipse.milo.opcua.stack.core.util.NonceUtil.validateNonce(NonceUtil.java:183)
at org.eclipse.milo.opcua.stack.core.util.NonceUtil.validateNonce(NonceUtil.java:153)
at org.eclipse.milo.opcua.stack.server.transport.uasc.UascServerAsymmetricHandler.openSecureChannel(UascServerAsymmetricHandler.java:437)
at org.eclipse.milo.opcua.stack.server.transport.uasc.UascServerAsymmetricHandler.lambda$sendOpenSecureChannelResponse$1(UascServerAsymmetricHandler.java:301)
at org.eclipse.milo.opcua.stack.core.channel.SerializationQueue.lambda$encode$0(SerializationQueue.java:57)
at org.eclipse.milo.opcua.stack.core.util.ExecutionQueue$Task.run(ExecutionQueue.java:119)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
we have an ant build and I believe all the classpaths are pointing to the current library.
You must have another, older, version of BouncyCastle that is also on the classpath or in your build somehow. The method causing this error was only added in BC v1.61.
We found that another jar pdfbox-all-2.0.13.jar had an older embedded version of bouncycastle in it and our ant build was unzipping those jars over the newer version of the required newer libraries. Removing the older bouncy castle classes or using a vanilla pdfbox appears to resolve the NoSuchMethod issue. Can't imagine this will be a common scenario for many others.
<jar jarfile="${dist}/thirdparty.jar">
<manifest>
<!--These jars need to be at the same level as the jar created for this project-->
<attribute name="Compiler" value="${build.compiler}"/>
</manifest>
<fileset dir="./src" includes="META-INF/persistence.xml" />
<zipfileset excludes="META-INF/*.SF" src="../../../filtec-bin/packages/rhino/lib/rhino.jar" />
<zipfileset excludes="META-INF/*.SF" src="../../../filtec-bin/packages/junit4/junit-4.12.jar" />
...
<zipfileset excludes="META-INF/*.SF" src="../../../filtec-bin/packages/bouncycastle/bcpkix-jdk15on-1.61.jar" />
<zipfileset excludes="META-INF/*.SF" src="../../../filtec-bin/packages/bouncycastle/bcprov-jdk15on-1.61.jar" />
...
<zipfileset excludes="META-INF/*.SF" src="../../../filtec-bin/packages/pdfBOX/pdfbox-app-2.0.13.jar" />
...
Below is my ant build jar target:
<target name="build jar" depends="compile">
<mkdir dir="${dist.home}" />
<jar destfile="${dist.home}/${app.name}.jar" basedir="${build.home}/classes">
<zipgroupfileset dir="${lib.home}" includes="*.jar" excludes="*.RSA, *.SF, *.DSA, META-INF/MANIFEST.MF" />
<manifest>
<attribute name = "Main-Class" value = "com.karthik.publisher"/>
</manifest>
</jar>
</target>
When I run/execute the jar from command prompt, I get the below error
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: no manifiest section for signature file entry javax/mail/SecuritySupport.class
at sun.security.util.SignatureFileVerifier.verifySection(Unknown Source)
at sun.security.util.SignatureFileVerifier.processImpl(Unknown Source)
at sun.security.util.SignatureFileVerifier.process(Unknown Source)
As you can see I excluded RSA, SF and DSA files and MANIFEST.MF from each jar but I am still getting this exception.
The excludes definition in your build jar task only covers .RSA .SF and .DSA files in the root of your jar file. However they are located in the META-INF subdirectory.
*.RSA-> covers RSA files in the root of the zip/jar file to be merged
**/*.RSA-> covers RSA files everywhere in the zip/jar file to be merged
META-INF/*.* -> covers all files in the META-INF directory
I would recommend to you to use the third alternative, as I don't see a reason for any jar to place a file in the META-INF directory of the merged jar.
I'm creating a JAR file for my project that uses Log4j2. I have saved the Log4j2 jars (for CORE and API) in the 'lib' folder under the 'src' folder. Within Eclipse, I exported my project to create the JAR file and I have checked that the 'lib' folder is included/present in the JAR file.
When I run the project from within eclipse, it runs absolutely fine but when I try to execute the jar via the cmd prompt, I get the following error:
C:\Users\workspace>java -jar ConnectorClient.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager
at com.client.ConnectionClient.<clinit>(ConnectionClient.java:20)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
The line in my code (Line 20) causing the error is:
private static Logger logger = LogManager.getLogger(ConnectionClient.class);
It seems as if the Log4j2 jars are not being read. Can somebody be able to help me here please. I've already added the 2 jars in the project's Java Build Path as shown in the image..
My classpath file (present inside the project folder) includes the 2 jars in questions as shown:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.8.0_05"/>
<classpathentry kind="lib" path="lib/log4j-api-2.0-rc1.jar"/>
<classpathentry kind="lib" path="lib/log4j-core-2.0-rc1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Any help please?
Thanks
Maybe you want to export a Runnable Jar file:
And include the required libraries:
There is a common misconception that a jar can be packaged into another jar. It just won't be available to the classloader, so you will end up with class loading exceptions.
So you either extract the dependency jar into your own jar, as described using eclipse by #PaulVargas, OR you do what 90% of people do and add your dependencies to the classpath of your jar execution:
java -classpath libs/*.jar -jar MyJar.jar
This means putting all your dependencies in a separate folder (here, libs) NOT packaged within your jar.
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.
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).