I'm fairly new to Java and I'm having trouble exporting my application into a runnable jar.
My application runs some transformations using Saxon and I want the stylesheets used to be inside the runnable jar.
Right now I simply have a subfolder in my project in eclipse and I'm accessing the stylesheets with that path.
However as soon as I export the project as runnable jar inside that jar all the stylesheets are on the same level as the used libraries (subfolder is nowhere to be found.)
Also the application searches for that subfolder in the cwd and of course can't find it there.
How do I pack the stylesheets and access them properly inside the jar ?
//Update:
I think I'm a little further.
I've created a subfolder under src and put all stylesheets in there. I'm accessing them like so:
xsltexps = comp.compile(new StreamSource(new File(this.getClass().getResource("/stylesheets/" + stylesheets).toURI())));
If i run it in eclipse everything works fine. When I export it as runnable jar and run it i get the following exception:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.(Unknown Source)
I have no idea what that even means.
Okay, so here's what actually worked:
xsltexps = comp.compile(new StreamSource(
getClass().getResourceAsStream("/stylesheets/" + stylesheets)));
Most importang change is the use of getResourceAsStream instead of getResource
Related
I am using IntelliJ to create a variant of the Java WorldWind application. When I build the JAR artifact in IntelliJ I can see my DLLs and SOs in the top-level directory of the JAR. However, when I try to execute it, it complains it can't see these libraries that are inside the JAR (tried both Windows and Linux, thus being why I include SOs and DLLs).
What do I need to do to get it to see these libraries?
This is my first time creating an executable JAR, so sorry for the vagueness, I'm not quite sure what information is important. Please ask me any questions necessary to better understand this problem, and I will answer them.
Edit:
Catched FileNotFoundException: D:\code\Animator\animator\out\artifacts\Animator_jar\Animator-natives-windows-i586.jar (The system cannot find the file specified),
while addNativeJarLibsImpl(
classFromJavaJar class com.jogamp.common.os.Platform,
classJarURI jar:file:/D:/code/Animator/animator/out/artifacts/Animator_jar/Animator.jar!/com/jogamp/common/os/Platform.class,
nativeJarBaseName Animator-natives-windows-i586.jar):
[ file:/D:/code/Animator/animator/out/artifacts/Animator_jar/Animator.jar ->
file:/D:/code/Animator/animator/out/artifacts/Animator_jar/ ] +
Animator-natives-windows-i586.jar ->
slim: jar:file:/D:/code/Animator/animator/out/artifacts/Animator_jar/Animator-natives-windows-i586.jar!/
Exception in thread "main" java.lang.UnsatisfiedLinkError: Can't load library: D:\code\Animator\animator\out\artifacts\Animator_jar\gluegen-rt.dll
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.load0(Unknown Source)
at java.lang.System.load(Unknown Source)
at com.jogamp.common.jvm.JNILibLoaderBase.loadLibraryInternal(JNILibLoaderBase.java:551)
at com.jogamp.common.jvm.JNILibLoaderBase.access$000(JNILibLoaderBase.java:64)
at com.jogamp.common.jvm.JNILibLoaderBase$DefaultAction.loadLibrary(JNILibLoaderBase.java:96)
at com.jogamp.common.jvm.JNILibLoaderBase.loadLibrary(JNILibLoaderBase.java:414)
at com.jogamp.common.os.DynamicLibraryBundle$GlueJNILibLoader.loadLibrary(DynamicLibraryBundle.java:388)
at com.jogamp.common.os.Platform$1.run(Platform.java:209)
at java.security.AccessController.doPrivileged(Native Method)
at com.jogamp.common.os.Platform.<clinit>(Platform.java:179)
at javax.media.opengl.GLProfile.<clinit>(GLProfile.java:82)
at au.gov.ga.worldwind.animator.application.AnimatorConfiguration.<clinit>(AnimatorConfiguration.java:46)
at au.gov.ga.worldwind.animator.application.Animator.<clinit>(Animator.java:205)
As far as the structure of the JAR, I don't know how to show other than a screenshot of the JAR in 7-Zip:
In the image we can see that the name of the jar is: Animator.jar, but intelliJ is searching for: Animator-natives-windows-i586.jar. You must go to Module Settings - Artifacts and change the OutputDirectory to: D:\code\Animator\animator\out\artifacts\Animator_jar
Then check the name of the artifact to match the path and the name of the file.
The image added by me is an example.
I am debugging a Java application that runs Groovy program for a certain database transaction call. When the pointer passes the following line of the Groovy program
sql.eachRow("select.....from...where...",[value0, value1])
It prints the following exception
- Servlet.service() for servlet Groovy threw exception
java.lang.NoClassDefFoundError: org/apache/commons/collections/CursorableLinkedList
at org.apache.commons.pool.impl.GenericObjectPool.<init>(GenericObjectPool.java:392)
at org.apache.commons.pool.impl.GenericObjectPool.<init>(GenericObjectPool.java:258)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:795)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
at groovy.sql.Sql$36.run(Sql.java:1192)
at java.security.AccessController.doPrivileged(Native Method)
at groovy.sql.Sql.createConnection(Sql.java:1190)
at groovy.sql.Sql.eachRow(Sql.java:466)
at gjdk.groovy.sql.Sql_GroovyReflector.invoke(Unknown Source)
at groovy.lang.MetaMethod.invoke(MetaMethod.java:115)
at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)
at org.codehaus.groovy.runtime.Invoker.invokeMethod(Invoker.java:119)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:111)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:187)
at sce_expiry$_run_closure1_closure4_closure5.doCall(sce_expiry.groovy:70)
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:592)
at org.codehaus.groovy.runtime.ReflectionMetaMethod.invoke(ReflectionMetaMethod.java:69)
at org.codehaus.groovy.runtime.MetaClassHelper.doMethodInvoke(MetaClassHelper.java:713)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:560)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnCurrentN(ScriptBytecodeAdapter.java:97)
at sce_expiry$_run_closure1_closure4_closure5.doCall(sce_expiry.groovy)
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:592)
sce_expiry.groovy is the groovy file name.
The solutions other people got recommended are like making sure the common-collections.jar and groovy-all.jar are in the classpath. I have verified. Both jar files are located in the lib folder of the webapp's directory inside my Tomcat. And the class can be found inside common-collections.jar with the same package name.
I actually have almost 0 experience with Groovy. So I'm really not sure whether some additional configuration is needed.
Does anyone have any idea?
--------------------------Solution found and question might change--------------
Alright, so I found out two ways to make this work. And they are related.
First of all, I am using Tomcat 5 here. In the Tomcat folder, there is a common/lib directory for users to place jar files shared by applications.
There are already some Apache commons-xxx.jar files in place but not commons-collection.jar. If I put a copy of commons-collection.jar together with the existing jar file, it will work! Alternatively, remove one jar file (commons-dbcp.jar), it will work as well. Both commons-collection.jar and commons-dbcp.jar are available in the application's own lib folder.
Yet, I need to do more research on this, but I suspect that commons-dbcp.jar need to work together with commons-collection.jar for the function I call, and that common/lib folder takes a higher priority to be read in. Therefore, once commons-dbcp.jar is read in, Tomcat will ingore the same in the app's lib folder and will only work with the one in the common lib folder. While commons-collection was not available in the common lib folder, it complained when commons-dbcp.jar was looking for it.
Please correct me if I'm wrong. I don't know perfectly how Tomcat works..
So this question is no longer tagged with Groovy and Grails, but Java and Tomcat 5.
I have project called test in Eclipse which I want to export into Runnable JAR file. During the process of exporting the project, when I choose as a location of JAR file the subfolder of test folder in file system, such as C:/.../test/App.jar, it will work. However, when I want to put it for example on my desktop, it will run but in a very limited look (some GUI components missing) and it throws a lot of exceptions saying something like that:
at java.awt.EventDispatchThread.run(Unknown Source) xception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: Could not initialize class com.sun.opengl.impl.windows.WindowsGLDrawableFactory
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at javax.media.opengl.GLDrawableFactory.getFactory(GLDrawableFactory.java}
What can be the problem? I assume it is not good that I have to specify my JAR file concretely into one folder in file system, it would become unuseful for any other potentional user.
Looks like the jar needs other jars to execute. In this case you would have to set the classpath before you execute the jar
This might help you with your problem.
I've been building an application in Eclipse which extracts data from salesforce and using an imported CSV file, updates and inserts records. It works fine from within Eclipse and I'm happy it's ready for deployment, but I cannot get the application working if I try and run it from cmd or double click the Jar. I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: uploader/jar
Caused by: java.lang.ClassNotFoundException: uploader.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: uploader.jar. Program will exit.
I've been all over google and tried every solution out there, but can't find why it won't work. Here's the steps I'm using to build the jar and run it, please if anyone can show me where I'm going wrong, I'd be delighted! I'm not a java programmer...
The project is called uploader as is the package, by the way
Right click on project and click Export. Choose "Jar file" and
Next.
Select my project as the resource and ensure .classpath and .project
files are selected. Choose to Export generated class files and
resources, to Compress the contents of the JAR file, and choose the
location for the Jar file to go, then hit Next.
Choose to Export class files with compile errors, and warnings.
Choose to Save the description of this JAR in the workspace, and
give the location as /uploader/jardesc.jardesc. Hit Next.
Choose to Generate the manifest file, Save the manifest in the
workspace and Use the saved manifest in the generated JAR
description file.
Point the Main Class to the correct place by clicking Browse and
choosing the only option that I'm given, which is uploader.Main,
then click Finish.
If I now open the manifest file, I see the following:
Manifest-Version: 1.0
Main-Class: uploader.Main
So that looks correct to me.
However, if I open a command prompt, cd to the directory containing the jar file then say java uploader.jar, that's when I get the error above.
Can anyone see anything that looks wrong? I can't get past this point!
If I run with java -jar uploader.jar, I get this error instead:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sforce/ws/ConnectionException
Caused by: java.lang.ClassNotFoundException: com.sforce.ws.ConnectionException
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: uploader.Main. Program will exit.
Perhaps that can help pinpoint the problem?
*EDIT*
I've edited the manifest.mf file to this, but I'm still getting the error:
Manifest-Version: 1.0
Main-Class: uploader.Main
Class-Path: lib/enterprise.jar lib/wsc-22.jar
The problem seems to be a missing classpath setting, i.e. a definition of the classes and libraries that are needed in addtion. In your case it seems like at least some salesforce jar has to be added to the classpath.
Either put the classpath into the manifest file or add it at the command line: java -cp <classpath here> -jar uploader.jar
I have a project which runs fine from within Eclipse.
But when I export it to a runnable JAR with dependencies packed into it it won't run.
The error, when running the JAR from console is:
EDIT (didnt run it with -jar before):
INFO: Loading XML bean definitions from class path resource [applicationContext-
framework.xml]
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOExc
eption parsing XML document from class path resource [applicationContext-framewo
rk.xml]; nested exception is java.io.FileNotFoundException: class path resource
[applicationContext-framework.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:349)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:310)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.AbstractXmlApplicationContext.loa
dBeanDefinitions(AbstractXmlApplicationContext.java:113)
at org.springframework.context.support.AbstractXmlApplicationContext.loa
dBeanDefinitions(AbstractXmlApplicationContext.java:80)
at org.springframework.context.support.AbstractRefreshableApplicationCon
text.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
at org.springframework.context.support.AbstractApplicationContext.obtain
FreshBeanFactory(AbstractApplicationContext.java:422)
at org.springframework.context.support.AbstractApplicationContext.refres
h(AbstractApplicationContext.java:352)
at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:93)
at com.horstmann.violet.UMLEditorApplication.getApplicationContext(UMLEd
itorApplication.java:111)
at com.horstmann.violet.UMLEditorApplication.<init>(UMLEditorApplication
.java:94)
at com.horstmann.violet.UMLEditorApplication.main(UMLEditorApplication.j
ava:84)
... 5 more
Caused by: java.io.FileNotFoundException: class path resource [applicationContex
t-framework.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPat
hResource.java:143)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:336)
... 20 more
but the file is in resources/ from the JAR-root.
What am I missing or doing wrong?
Try to click on the option “Package required libraries into generated JAR”.
A JAR file is a regular compressed archive. Make sure that all the required libraries in your MANIFEST.MF file are present within the generated file.
Try to run the JAR from the command line - it will most likely display a useful error message.
There probably are some dependencies that eclipse doesn't know about or which aren't configured correctly within it, and are therefore missing from the JAR.
In general, it's not a good idea to rely on IDE functionality to produce deliverables - it depends on too many hidden factors and can't be automated for a build server. Any project that's beyond the experimental/toy stage should have an automated build via Ant or Maven.
The message
Could not find the main class: violet-0.21.2-SVN.jar. Program will exit.
indicates that you have gotten the Main-Class: MyPackage.MyClass line wrong in the Manifest when packaging together the jar file. You need to give the full name of the class with the main(String[] args) method.
See http://download.oracle.com/javase/tutorial/deployment/jar/appman.html for details.