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
Related
I have a small Java project built with the jGRASP IDE on Windows, with all the files in a single flat directory. Among them is an external jar file: pdfbox-app.jar. Compilation and access to that library succeeds if I add the file to the project CLASSPATH with an absolute address.
But the project build then breaks if this is distributed to other people (via GitHub) and they put the code in any different directory structure. So it would be preferred to provide a relative address on the CLASSPATH. However,nothing I've tried to add in that vein reliably succeeds. Things I've tried to add to the project classpath:
pdfbox-app.jar
\pdfbox-app.jar
.\pdfbox-app.jar
%<PROJECT_PATH>\pdfbox-app.jar
I've also tried to add command-line compile arguments:
-cp ".;.\pdfbox-app.jar"
What would work here for a relative-address inclusion on the jGRASP project CLASSPATH?
Any of those classpath additions should work, assuming that pdfbox-app.jar is located at the root of your common classpath (the directory containing the package roots), or for %<PROJECT_PATH>, the directory containing the project file itself if that is different. If it is not, then you need a relative path to one of those locations.
The flags should also work, but for Java flags you need to add them as "Flags2 or Args2" for the "Compile", "Run", and "Debug" commands using "Settings" -> "Compiler Settings". Command line arguments are arguments to your program only.
I've been given a .jar that calls to a .dll to use in a project.
I can compile and run without problem if I do the following:
Add the folder containing the .dll to the Path environment varibale (Windows 10).
Add the .jar as a dependency on my gradle project.
That way the API works fine.
When I move the generated jar to another computer it cannot find the dll although I include it in the jar, so the software doesn't work.
How can I tell the generated jar that the dll it needs is in it?
I feel like the .jar they sent me just calls the functions of the dll and assumes you added the directory to the path variable. Is there any way to make this work without needing to change the path configuration to every PC I move the jar?
I´ve already tried the answers posted here (And they don't work):
Gradle how to add native dependency? [Libgdx]
Add native library to local jar in gradle build
The problem is the way the jar loads the dll. Usually it just calls System.loadLibrary(<libname>) which expects the native library to be in the library search path.
If you want to load it from somewhere else you have to extract the DLL from the JAR into e.g. a temp directory or somewhere else and the load it using
System.load(<absolute path to the dll file>);
Note: If you can't change the Classes in the JAR you can place the call to System.load(..) in a second class that is loaded before the code of the JAR is executed. AFAIR a library is only loaded once by Java, therefore if the dll of the same name is already loaded the call to System.loadLibrary() in the original JAR is simply ignored.
My application relies on two external libraries, I have both in jar format and have added them to my classpath, making it possible to run my application within NetBeans.
However, I would like to package my application in an easy to use jar file. When I tried the automatic method of jar creation provided by NetBeans (where it auto generates a jarfile in dist/) and ran it on another computer, I got lots of ClassNotFound (or similar) exceptions for classes that I could tell were supposed to be provided by my other libraries.
Is there a way I can include the other jarfiles I have into my own jar? I've never created an application which relies on other libraries before so this is a first for me.
You can add a "Class-path" line to your jar's manifest. The drawback is that you have to hard code the paths to a file system (not jar) location in the manifest. If you put them all in the same directory or a consistent relative directory, it should be manageable.
See: http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
Alternately you can try something line One-JAR: http://one-jar.sourceforge.net/
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
I've looked through many of the existing threads about this error, but still no luck. I'm not even trying to package a jar or use any third-party packaging tools. I'm simply running from within Eclipse (works great) and then trying to run the exact same app from the command line, in the same location it's built to (getting this error). My goal is to be able to zip up the bin folder and send it off to be run by someone else via a command line script. Some details:
It's a command-line app and I'm using the commons-lang-2.4.jar for string utilities. That is the file that cannot be located (specificaly "java.lang.NoClassDefFoundError: org/apache/commons/lang/StringEscapeUtils")
I have that jar in my lib folder and have added it to my build path in Eclipse via right-click "Build Path -> Add to Build Path"
The .classpath file looks correct and contains the reference to the jar, but I assume that file is only used by Eclipse (contains this line: <classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>)
Could this be related to the Eclipse working directory setting? I have some internal template files that I created that are under src/templates, and the only way I can seem to get those to be seen is by setting the project working directory to AppName/src. Maybe I should be putting those somewhere else?
Let me know if any additional info would help. Surely this is something simple, but I've wasted too much time on it at this point. This is reminding me why I originally left Java back in '05 or so...
A NoClassDefFoundError basically means that the class was there in the classpath during compiletime, but it is missing in the classpath during runtime.
In your case, when executing using java.exe from commandline, you need to specify the classpath in the -cp or -classpath argument. Or if it is a JAR file, then you need to specify it in the class-path entry of its MANIFEST.MF file.
The value of the argument/entry can be either absolute or relative file system paths to a folder containing all .class files or to an individual .jar file. You can separate paths using a semicolon ;. When a path contains spaces, you need to wrap the particular path with doublequotes ". Example:
java -cp .;c:/path/to/file.jar;"c:/spacy path/to/classes" mypackage.MyClass
To save the effort of typing and editing the argument in commandline everytime, use a .bat file.
Edit: I should have realized that you're using an Unix based operating system. The above examples are Windows-targeted. In the case of Unix like platforms you can follow the same rules, but you need to separate the paths using a colon : and instead of an eventual batch file, use a .sh file.
java -cp .:/path/to/file.jar:"/spacy path/to/classes" mypackage.MyClass
Are you specifying the classpath to java on the command line?
$ java -cp lib/commons-lang-2.4.jar your.main.Class
The classpath setting you are setting in Eclispe are only for the IDE and do not affect how you application is run outside the IDE. Even if you use the Eclipse Functionality to export your application as an executable jar file there is no out of the box way to package all the jars your application depends on.
If you have packaged you application into a jar file called myapp.jar then running a command like below will run the application with the jar you depend on, if you have more than one just add them separted by ; on Windows or : on Unix:
java -jar myapp.jar -cp .;c:/pathtolibs/commons-lang-2.4.jar
If you are just running the classes directly then either run the folder containing your .class files will also need to be on the path (though I assume it already is since you are able to run the program and get errors).
Consider File -> Export -> Runnable jar to create a jar file which can be invoked directly with
java -jar yourProgram.jar
There are several variants depending on your needs.
Eclipse does not move any of the jars in your classpath into the bin folder of your project. You need to copy the util jar into the bin folder. If you move it to the root of the bin folder, you might be able to get away without any classpath entries but it's not the recommended solution. See #BalusC's answer for good coverage of that.
Eclipse doesn't build executable java classes by default. Don't ask me why, but it probably has something to do with using their own tools.jar (somewhere in plugins/org.eclipse.core ?) so that Eclipse can run without a JDK.
You can usually go to your project bin directory and do:
java -cp . MyClass
But if you have external jars, Eclipse handles those internally in another weird way, so you'll need to add those too.
make sure your jar commons-lang-2.4.jar in classpath and not redudance.
I ever add jar file to my classpath, and have 2 file jar in my classpath. After I delete it, work smooth