Java VM Options: Set a -Djava.library.path relative to project folder - java

I would like to set the -Djava.library.path VM option to a specific folder. However, it is not relative to my project folder so that I could say for example:
-Djava.library.path=native\windows
(The folder native is in the project folder.)
Do you know if this is somehow possible to set it like above and not with -Djava.library.path=C:\...?

You actually can set a relative path. For example if you start your program a specific folder, you can access libraries in a folder "libs" right next to it by setting the path to "../libs" e.g.
In my own project with native libraries I have this in my shell script:
-Djava.library.path=../../native/unix
Hope this answers your question.

I'm not sure if you are asking how to refer to a relative directory on windows, or how to set this path without the -Djava.library.path=... parameter. So, I will answer both.
To set a relative path, use:
-Djava.library.path=.\windows
To set this path on Windows without using -D, augment the PATH environment variable:
setenv PATH %PATH%;C:\path\to\folder
On Linux/Mac, set/augment the LD_LIBRARY_PATH with this folder location.

Related

Must I place all dependent DLLs into the JDK's bin folder?

My java application depends on a DLL, and that DLL further depends on libstdc++-6.dll.
I tried to:
placed the libstdc++-6.dll in a folder
and put the folder in the %PATH%
Then I meet the java.lang.Unsatisfied LinkError: The specified procedure could not be found when launching application from Eclipse.
But if I put the libstdc++-6.dll into the JDK's bin folder, say C:\Java\jdk1.6.0_45_32bit\bin. It works fine.
But I don't want to pollute the JDK folder. I remember windows will search %PATH% to locate dependent DLLs. Why can't I use the %PATH% in this issue?
Update 1
There are 2 different %PATH% environment variables in Windows.
User variables
System variables
I just accidentally find that:
If I put the DLL's folder to User %PATH%, it cannot be found.
If I put the DLL's folder to System %PATH%, it works.
Why?
Update 2
Inspired by this thread:System versus user PATH environmental variable...winmerge works only if I add the path to the user PATH
I start to wonder maybe my User %Path% is too long. So I moved the folder path containing my dependent DLL from the end of User %PATH% to the beginning. It works now!
At first, I conclude that one who implemented the Windows' DLL lookup algorithm has some truncation issue. And I almost consider it as another annoying Windows Bug.
But I wrote another Windows application which has similar DLL dependencies to confirm my guess. That application works fine! So I have to review my conclusion.
I checked my User %PATH% entry one by one, and place the folder to each possible location. And finally, I find the root cause.
I have a C:\MinGW\bin entry in User %PATH%, which happens to contain a
libstdc++-6.dll (977KB) but unfortunately, which isn't compatible
with the one I need (825KB). It only works if I place my folder before MinGW. So it's actually DLL collision during %PATH% resolution.
Now this issue seems resolved. But another one comes up, do I need to switch back and forth if I want to use both my DLL and the MinGW?
Update 3
Please check the comment by #AndyThomas. He mentioned using System.loadLibrary() for both direct and indirect DLLs. This way, all we need to care about is the java.library.path property. I think that's a once-for-all solution.
First: put all DLL files you need in the same directory
Then: Load native libs - to do so you have 3 options:
Set VM Options while you run your app.
-Djava.library.path="C:\Your Directory where Dll is
present"
Example:
java -Djava.library.path="C:\Your Directory where Dll is
present" -jar app.jar
Load specific native library from within the app:
a) Place the directory that contains the file aaa.dll directly under the Java project.
b) And place this line on the top of stack trace of your app: System.loadLibrary("aaa")
Use VM options from within your app:
System.setProperty( "java.library.path", "C:\Your Directory where Dll is
present" );

Add custome utility JAR to Windows System Path

Is it possible to add a utility jar to Window's System Path?
I have findclass.jar that is an exicutable jar located in my C:\ directory.
If I open cmd.exe, change directories to C:\, and type:
java -jar findclass.jar \path\tojar.jar nameofclass.class
it will search another jar for a given class.
How can I add findclass.jar to my Window's System Path so I don't need to change directories to C:\?
I tried set path=C:\;%PATH% and set path=C:\findclass.jar;%PATH%
but I get Error: Unable to access jarfile findclass.jar when I try to execute it from a different directroy.
Is it not possible to add a .jar file to the path?
Add it to CLASSPATH environment variable.

Need to set path as a system property for java project

I have 2 property files which are part of my java eclipse project. Now i need to pass the path of these 2 files as a system property like -Dpath1="pathfile1" as project will only accept it as a system property.
One option is that i can specify the absolute path but i want to specify a path relative to my project something like -Dpath1=$Project_Dir/resource/file1. Reason being, if project directory is moved then project will start failing.
I am using eclipse to run this project and dont want to declare any new system variable for supporting relative path, so for example dont want to create a new system variable Project_Dir . Is it possible to achieve this and how?
You can set system properties with:
System.setProperty("path1", yourPathHere);
Now, you only have to build the correct path, relative to where your project is. To do this, start off with the location of the project: How to get the path of a running JAR file?
When using setProperty, you can reference internal project files if you use a plain relative path.
E.g. to reference a file located at /abc/def/workspace/project/lib/driver.exe:
System.setProperty("driver", "lib/driver.exe");
It's deceivingly simple.
Yes, you can do it.Eclipse have build-in Path variable PROJECT_LOC, it stores location of project folder, e.x. c:\Workspace\Project1.
You can use this property in launch configuration.

setting class path in Java

I used to run eclipse on my laptop. I have installed and used Secunia PSI application in order to update the apps, but once I done that I cannot run Eclipse.
This is what I have:
I set the PATH to the directory which contains javaw.exe app. The path at the PATH variable is the same as of CLASSPATH variable.
So, what are your resolutions? Done, it works now. I just simply needed to add '.;' at the end of the path link at the PATH.
Cheers
As the error message clearly states, Eclipse cannot find the entire JVM.
Add the directory that contains javaw.exe to your PATH.
Better to add JAVA_HOME as a new system variable in the same screen where you see the class path by clicking New and providing the folder location for JAVA root installation folder e.g. below:
Variable Name= JAVA_HOME
Variable Value= C:\Program Files\Java\jdk1.7.0_09

Gdal for windows 7(x32)

How do I connect to the gdal eclipse. I tried all the ways it does not work.
How do I connect to the gdal eclipse. I tried all the ways it does not work.
All the time issue
java.lang.UnsatisfiedLinkError: D: \ Document \ JAVA \ Gdal \ gdalLibrary \ gdaljni.dll: Can't find dependent libraries
I also have this problem and just found a solution. I am working with WIN8, and downloaded release-1600-gdal-1-8-0-mapserver-5-6-6.zip. As in the readme txt, I run the SDKShell.bat to set the environment. Then I include the jar and the four jni dlls in the java folder into my testing java project. There was always error like gdaljni.dll: Can't find dependent libraries. Then I figured out the settings in the bat may not been successfully done. I opened up the bat with an editor and found all the necessary enviroment settings and did it one by one. Then the java project worked with no problem. The settings needed are as follows:
SET PATH=%CD%\bin;%CD%\bin\proj\apps;%CD%\bin\gdal\apps;%CD%\bin\ms\apps;%PATH%
SET GDAL_DATA=%CD%\bin\gdal-data
SET GDAL_DRIVER_PATH=%CD%\bin\gdal\plugins
SET PROJ_LIB=%CD%\bin\proj\SHARE
For the PATH you can just in explorer right click on computer-properties-advanced system settings-Change enviroment variables. Add the absolute paths(%CD% means the current folder of the bat file, in my case is C:\GDAL, so path "%CD%\bin" should be changed into "C:\GDAL\bin") of "SET PATH line" to your PATH. For the other three lines of settings, just add three system variable for each and aslo change the path containing %CD% into abousolute paths. Then, everything should be ok. Note I omitted some paths in the bat file which I do not need for my java bindings. You can also add them to PATH if you want to do use gdal with other languages. I guess the most common mistake is forgetting to add the GDAL_DRIVER_PATH and PROJ_LIB.Hope it may be helpful for the others who have such issue.
If UnsatisfiedLinkError: gdaljni.dll: Can't find dependent libraries occurs...
Check the path of GDAL libraries, data and plugins (they must be in the installation path of your application)
Missing VC++2010 runtime (if GDAL is compiled using VC++2010): download the Microsoft Visual C++ 2010 Redistributable Package:
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=a7b7a05e-6de6-4d3a-a423-37bf0912db84
You may need to set following in the Windows system environment variables:
GDAL_DATA=<path to gdal-data folder>
GDAL_DRIVER=<path to your gdal\plugins folder>
This might help you:
1) set path as said above
2) Put the *jni.dll files in the 'The path where you installed gdal'\gdallib\bin\gdal\java\ directories into the jre\bin directories that you are using. (This saved me!)
Hope it will be helpful:)

Categories