How to customize java options of a independent netbeans application? - java

i'm developing an application using Netbeans RCP. I have added an option to add a jar to my class path in the project.properties file of my platform:
run.args.extra=-cp:a ./appclient/glassfish/lib/gf-client.jar
The problem i encounter is that is does work when i run it from the Netbeans IDE but not when i try to create a independent application (build for Mac OSX for instance). I hear that the project.properties is no longer taken in account when you run an independent application and of course my appclient directory containing the jars does not exist anymore in the application package (so my jar is not added to class path).
How can i make this -cp option works for my independent Mac OSX application?
EDIT: i was able to create a custom conf file for my independent platform but i can't find a way to add my jar to the class path, i don't know what options to use.
EDIT: i found that i need to you endorsed mechanism to achieve it. So i have added the following command to my app.conf file:
J-Djava.endorsed.dirs=/Users/altanis/appclient/glassfish/lib/gf-client.jar
But when i run the .app (mac application), i get this error:
-J-Djava.endorsed.dirs=/Users/altanis/appclient/glassfish/lib/gf-client.jar: No such file or directory
The path is correct. Do i need to make something special to make the JVM aware of this? I followed this tutorial and somewhere in the comments the author says:
Right, but the package-appclient copies everything for you and you
should be able to put it on the classpath using the endorsed
mechanism. Unpack the jar created by that and add everything you need
from there (the jars) to your application installer. Then you can use
the endorsed (-J-Djava.endorsed.dirs=${GFCLIENT_PATH}) mechanism in
your app.conf to put it on the application classpath. This way you
should be able to deploy it together with your client.

I think, that create a new library is the better way.
Create module type library with required jars
In your module add dependency to created module (type wrapped library)

You must add entry Class-path to you application's MANIFEST.MF
For example
Class-Path: apache-commons-2.1.jar ejb-api-3.0.jar
all this jars should be in the root directory of your application
Your appliction should have next structure
MyApplication.jar
/META-INF
/META-INF/MANIFEST.MF
/apache-commons-2.1.jar
/ejb-api-3.0.jar
/com/package/classes
or you can use jar tool of JDK to create a jar
read more here Oracle doc

Related

How does Eclipse set Native.loadLibrary path?

I inherited a working large Java8 project that needs to be re-architected. It builds a jar, but also includes an example main (not included in the jar) and a deep tree of libs including some .dll files deep in the libs/ tree (dll's also NOT included in the jar).
At some point, the code in the jar calls Native.loadLibrary("fti2x.dll", fti2xWrapper.class, [Map< String, Integer>] options) on the simple file name "fti2x.dll" -- with no path components -- and the load succeeds. This .dll is only located deep in the libs/ directory on my computer. How did the jar, running under eclipse, figure out this was in /libs/client/usb/CommLib/amd64/fti2x.dll without specifying the path to it?
If I copy just the jar, the example main, and the libs/ directory to a new project, how do I configure it in Eclipse(Mars) to find the .dll for the Native.loadLibrary call? (Just simply doing so fails, so I am missing some config.)
How do I do execute the main.class outside of Eclipse and have it find the .dll?
Is there possibly some Java code I am missing that sets this path? If so, what do I look for?
There is a system property that is used to specify the path that JNA uses to search for native libraries: jna.library.path. Try printing out its value just after the library loads successfully in the old project to see if it is being set.
This answer explains how to set a system property in an Eclipse run configuration. Outside of Eclipse, specify a value for it using the -D command-line switch to the JVM, e.g.
java -Djna.library.path=<some path> YourMainClass

How to access the jar placed inside lib\ext directory in Eclipse?

I've made a jar library and places it inside jdk and jre's ext directory to check for installed extension scenario in Java.
Now when I a trying to create a Java project in eclipse and try to use the jar files classes or use the library, I am not able to access but those jar files are visible under Java PRoject jre system library in ECLIPSE.
Are the extension classes loaded when you run something outside of Eclipse? For example you can try the following command in a terminal to check if a class from the extension can be found:
javap name.of.ExtensionClass
When you run your program from Eclipse, are you sure that you use the JRE that has the extension installed?
You probably will have to specifically reference the jar in Eclipse. The colleciton of JARs that come with the JRE are also included in the manifest file so that the runtime knows which files to request be loaded. Since your jar is not in the manifest, it will not be loaded automatically.
An alternative might be to repackage a JRE with an updated manifest that refers to your extension jar.
After adding to JAVA_HOME/lib/ext file or java.ext.dirs system property,
Don't Run the jar file ,it won't.
but Run the class with package name ,it will.

Adding comm.jar into my jar

I add comm.jar library into my .jar and I get javax.comm.NoSuchPortException.
Is this bug because I haven't add javax.comm.properties and win32com.dll into my jar? How can I add this file inside manifest?
Java does not support Jars within Jars. The correct way to ensure the javax.comm packages are on the run-time class-path of the application is to add a manifest that provides a relative path to the dependent Jar, and ensure that Jar is in the right place to be found. For details see Working with Manifest Files: The Basics & particularly Adding Classes to the JAR File's Classpath in the Java Tutorial.
There are other methods that can be used for applets and apps. launched using Java Web Start.
#Gogoo's answer says to copy stuff into your JVM installation directory.
It should work, but it is generally not a good idea:
The stuff that you install that way will be shared by all applications that you run using that installation. Depending on the what it is, it may interfere with other applications.
Each time you update your JVM, you have to remember to copy those files into the new installation directory.
IMO, a better idea is to put those files in a separate directory tree, and write a wrapper script to launch your application with the files on the classpath and library path. And/or see #Andrew Thompson's answer.
Try FatJar.
The Fat Jar Eclipse Plug-In is a Deployment-Tool which deploys an
Eclipse java-project into one executable jar.
It adds the Entry "Build Fat-JAR" to the Export-Wizard. In addition to
the eclipse standard jar-exporter referenced classes and jars are
included to the "Fat-Jar", so the resulting jar contains all needed
classes and can be executed directly with "java -jar", no classpath
has to be set, no additional jars have to be deployed.
copy comm.jar \jdk1.6\jre\lib\ext
copy win32com.dll \jdk1.6\bin
copy javax.comm.properties \jdk1.6\jre\lib
and start your.jar over command line:
java -jar your.jar

NoClassDefFoundError with Executable Jar

Probably a very usual problem with exec jar. I am trying to create and run an executable jar and its driving me crazy.
I have couple of classes(with package stmts), one of which has a main method, a simple empty constructor and ofcourse few biz methods, these form part of a small eclipse project.
I am bundling both these and a manifest file into a jar(arc.jar) using a build.xml. My program uses jdom library and also references from one of my other eclipse projects, so I am including both the jdom libray and other biz library while building my arc.jar.
This is the Manifest.MF I wrote, there is a new lineafter Main-Class
Manifest-Version: 1.2
Class-Path: jdom.jar other.jar
Main-class: uk.co.Art
When I unzip this arc.jar, it contains jdom.jar, other.jar, META-INF/Manifest.mf and the package with my classes.
Funnily, the Manifest file in jar looks like this -
Manifest-Version: 1.2
Created-By: 14.2-b01 (Sun Microsystems Inc.)
Main-class: uk.co.Art
Class-Path: jdom.jar other.jar
I got the ClassNotFoundException ClassNotFoundException: uk.co.Art when the value of Main class was without the package. With package the error changed to ClassNotFoundException: org.jdom.JDOMException.
So just for testing purpose, I tried giving the fullpaths with c:\ for both my executing class and the jars but still it is not able to locate the main class, errors out ClassNotFoundException: uk.co.Art.
I cant seem to understand why.
The command line I am using is - java -jar Arc.jar
The version on cmd prompt is -
java version "1.6.0_16"
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
Java HotSpot(TM) Client VM (build 14.2-b01, mixed mode)
while the eclipse installed jre is jre6
What can you folks suggest ?
Alex' solution did solve the problem for local execution.
I am also thinking of another way(as an extra option) of execution, to deploy the Arc.jar on server and run a batch on that machine which basically sets the java classpath and then calls either the jar or directly the class whichever way it works. But its only an option, not a preferred one though.
Or, also, set that classpath in my manifest and build jar locally and deploy on the server.
Would like to try out a few things though. But now I understand that the basic problem was jars into jar and hence classpath issue.
Thanks a lot folks.
You cannot bundle other JAR's in a JAR. They have to go in the same folder or at least in a folder relative to the Arc.jar. You specify then its relative path in the Class-Path entry.
Eclipse however has an useful export tool which allows you to embed the contents of other JAR's in your JAR so that you don't end up with multiple loose JAR's which are required to be placed in a specific location. Check the 2nd Library Handling option when you choose Export > Runnable JAR file.
When choosing Package required libraries into generated JAR, then Eclipse will add a special classloader which will load those JAR's transparently for you before executing the main class. It's doing that with help of JarRsrcLoader.
You do not have to pack your additional jars into your jar. These jars should be in the same directory as your application jar. By other words: put all jars into one directory and run. Everything will work.
Reasons: think what does executable jar mean. If you jar is not "executable" you run it as following:
java -cp art.jar;ldom.jar;other.jar uk.co.Art
when your jar is executable you use the following command line:
java -jar art.jar
No magic: jvm opens manifest and takes name of main class and other jars from there and virtually creates command line like first one. Now you understand that all jars in your case must be in the same directory.
Maybe the .jar is corrupt, like sometimes happened to me. Take a look at this.
Just couple of things to be noted when exporting.
JRE associated to the project is consistent with the JRE that would be used to run it.
Please export your project into a local drive, then move it into any shared drive (this fixed my issue).
Thank you.

Where is the debug class path in netbeans / where does the spring config XML file go?

I have a java desktop app (main project) and another project with a series of packages in NetBeans. Some of the packages use spring for JDBC and IOC.
I am getting the following error when running in debug:
Caused by: java.io.FileNotFoundException: class path resource [config.xml] cannot be opened because it does not exist
Where is the config file supposed to go? Where exactly is the class path? Is it in dist, build, in the root of the project that calls spring, or the main project (the desktop app)?
confused ..
Your classpath is defined when you run your app using the java command. You can specify it using:
java -cp $path my.Main
where $path is your classpath. It is a :-separated (; on windows) list of JAR files and/or directories containing compiled .class files.
If you run your program like:
java -cp configdir my.Main
And put your spring config in configdir (the fully-qualified path) then that should be discovered.
NetBeans: whilst I'm not a netbeans user, it probably offers a number of ways for you to complete the task you want:
In your run configuration (i.e. where you define what class is being run, what the command-line parameters are etc), you will probably be able to add items to the classpath. These might be directories or individual files
In your compiler settings, you can probably tell NetBeans to automatically copy files of a certain type (like properties files, XML config files) from your source locations to where NetBeans puts your class files.
If you put your config.xml file in the directory where NetBeans is compiling your .class files to
Put it in the root folder of you application
if you created your application in a folder called Spring then you should put your file in that folder
Disregard the answer by oxbow_lakes. NetBeans modifies CLASSPATH, so what it is outside the IDE is no measure of what it is inside the IDE.

Categories

Resources