Why doesn't a runnable JAR exported from Eclipse work? - java

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.

Related

Properly create Java JAR

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.

Packing Ressources into Jar and accessing them

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

JAR file behaving differently in a different place in file system

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.

"Could not find the main class" in Java Salesforce application

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

Trying to Use ESAPI But getting Error

I am trying to use ESAPI.jar for providing security to my web application.Basically I have just started using ESAPI.jar.
But problem is I am not able to run even a simple program using ESAPI.
The small code snippet is:
String clean = ESAPI.encoder().canonicalize("someString");
Randomizer r=ESAPI.randomizer();
System.out.println(r);
System.out.println(clean);
I get this error:
Attempting to load ESAPI.properties via file I/O.
Attempting to load ESAPI.properties as resource file via file I/O.
Not found in 'org.owasp.esapi.resources' directory or file not readable: D:\Eclipse-Workspace\Test\ESAPI.properties
Not found in SystemResource Directory/resourceDirectory: .esapi\ESAPI.properties
Not found in 'user.home' (C:\Documents and Settings\user.user) directory: C:\Documents and Settings\user.user\esapi\ESAPI.properties
Loading ESAPI.properties via file I/O failed. Exception was: java.io.FileNotFoundException
Attempting to load ESAPI.properties via the classpath.
ESAPI.properties could not be loaded by any means. Fail. Exception was: java.lang.IllegalArgumentException: Failed to load ESAPI.properties as a classloader resource.
Exception in thread "main" org.owasp.esapi.errors.ConfigurationException: java.lang.reflect.InvocationTargetException SecurityConfiguration class (org.owasp.esapi.reference.DefaultSecurityConfiguration) CTOR threw exception.
at org.owasp.esapi.util.ObjFactory.make(ObjFactory.java:129)
at org.owasp.esapi.ESAPI.securityConfiguration(ESAPI.java:184)
at org.owasp.esapi.ESAPI.encoder(ESAPI.java:99)
at org.rancore.testJasp.TestEsapi.main(TestEsapi.java:59)
Caused by: 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.owasp.esapi.util.ObjFactory.make(ObjFactory.java:86)
... 3 more
Caused by: org.owasp.esapi.errors.ConfigurationException: ESAPI.properties could not be loaded by any means. Fail.
at org.owasp.esapi.reference.DefaultSecurityConfiguration.loadConfiguration(DefaultSecurityConfiguration.java:439)
at org.owasp.esapi.reference.DefaultSecurityConfiguration.<init>(DefaultSecurityConfiguration.java:227)
at org.owasp.esapi.reference.DefaultSecurityConfiguration.getInstance(DefaultSecurityConfiguration.java:75)
... 8 more
Caused by: java.lang.IllegalArgumentException: Failed to load ESAPI.properties as a classloader resource.
at org.owasp.esapi.reference.DefaultSecurityConfiguration.loadConfigurationFromClasspath(DefaultSecurityConfiguration.java:667)
at org.owasp.esapi.reference.DefaultSecurityConfiguration.loadConfiguration(DefaultSecurityConfiguration.java:436)
... 10 more
I have tried copying the 3 ESAPI properties files in my source folder and also configuring them on build path but still I have not succeeded. I have tried many permutations and combinations to no avail.
Please guide me.
The content of property file is:
# User Messages
Error.creating.randomizer=Error creating randomizer
This.is.test.message=This {0} is {1} a test {2} message
# Validation Messages
# Log Messages
The ESAPI.properties file should have more than 3 lines in it. See for example:
https://web.archive.org/web/20150904064147/http://code.google.com:80/p/owasp-esapi-java/source/browse/trunk/configuration/esapi/ESAPI.properties
In my experience the ESAPI.properties file either needs to be in the same folder as the esapi.jar or needs to be compiled into the jar in a resources directory.
/resources/ESAPI.properties
I believe that either one should work. If ESAPI does not find the file it one location it looks in others.
The code for that is here around line 620:
https://web.archive.org/web/20161005210258/http://code.google.com/p/owasp-esapi-java/source/browse/trunk/src/main/java/org/owasp/esapi/reference/DefaultSecurityConfiguration.java
I just struggled through this one myself.
I created a folder called esapi in my C:/users/myname/ directory and loaded up the ESAPI.properties, validation.properties, and the ESAPI-AccessControlPolicy.xml which got me past all the not finding files errors. ESAPI looks in several places for the files. I'm running Windows 7 64 bit by the way.
Then I had to update some of the jars. I should have done this from the beginning, but I didn't know it. I was getting this error:
AccessController class (org.owasp.esapi.reference.DefaultAccessController) CTOR threw exception.
for every jar that needed to be a newer version, like commons collections or log4j. At the bottom of the stack trace it would reference the offending jar.
When I added the newer jars from the esapi lib directory everything just worked!
I got this message at the end in my console:
ESAPI.accessController found: org.owasp.esapi.reference.DefaultAccessController#1cb8deef
Note: There is an ESAPI_en_US.properties file, but it's the one with only a few lines in it. Just use the file in: \esapi-2.1.0-dist\src\test\resources\esapi\ESAPI.properties. That is the full complete file.
After having this problem and looking at the installation documentation (esapi-x.x.x-dist\documentation\esapi4java-core-x-x-install-guide.pdf) I found a very useful section which detailed that the properties file can be anywhere, provided a vm flag (-Dorg.owasp.esapi.resources=path") points to a particular directory.
For example, if I stick the file in a "resources" folder at the root of my project directory, then the flag would be:
-Dorg.owasp.esapi.resources="path\to\project\root\resources"
The reference SecurityConfiguration manages all the settings used by the ESAPI in a single place. In this reference implementation, resources can be put in several locations, which are searched in the following order:
1) Inside a directory set with a call to SecurityConfiguration.setResourceDirectory( "C:\temp\resources" ).
2) Inside the System.getProperty( "org.owasp.esapi.resources" ) directory. You can set this on the java command line as follows (for example):
java -Dorg.owasp.esapi.resources="C:\temp\resources"
You may have to add this to the start-up script that starts your web server. For example, for Tomcat, in the "catalina" script that starts Tomcat, you can set the JAVA_OPTS variable to the -D string above.
3) Inside the System.getProperty( "user.home" ) + "/.esapi" directory (supported for backward compatibility) or inside the System.getProperty( "user.home" ) + "/esapi" directory.
4) The first ".esapi" or "esapi" directory on the classpath. (The former for backward compatibility.)
Can you put your file (with this name) in:
D:\Eclipse-Workspace\Test\ESAPI.properties
And show us the contents and exception again.
Extract the esapi jar
create a folder named resources under org.owasp.esapi
copy ESAPI.properties to the org.owasp.esapi.resources
Build and deploy
extract the jar
add properties file under resources folder.
initially getting same error, after updating properties file it worked for me
Thanks for providing the information
-Dorg.owasp.esapi.resources="path\to\project\root\resources"
This is a good source of information and has resolved my issue
I had the same problem too. I resolved it using a little bit of James Drinkard solution. What I basically did is created a new folder with name ESAPI and added ESAPI.properties file, Validation.properties, and the ESAPI-AccessControlPolicy.xml. And archived it into a jar file and added to the lib folder under WebContet/WEB-INF and build it to the path and it worked.
*To archive it into a jar file I used windows command move ESAPI ESAPI.jar
Look at the various scripts under 'src/examples/scripts' and they will show you one easy way to control where the ESAPI.properties files is found. (This is for ESAPI 2.0 or later.)
You will find a copy of the ESAPI.properties file will be under 'configuration/esapi'.

Categories

Resources