I am trying to run my Java program but getting an error about a .so not existing.
I have specified the libs folder by using -Djava.library.path=libs and -DLD_LIBRARY_PATH=libs
As a result, when I run the file the console spits out the correct path of the file but says No such file or directory.
It's frustrating because the file does exist.
I have also tried running the application as sudo with no luck.
How can I get Java to recognise that this file does indeed exist?
Try explicitly setting the java.library.path explicitly in the code, this has sometimes worked for me where command line flags have failed
System.setProperty("java.library.path", path);
or try setting the path explicitly, in quotes
-Djava.library.path="/full/path/to/lib-folder"
Related
I´m running some test and I need to get the path of a file that it´s in a jar lib that I have in my project as dependency.
This jar is not part of the classpath that I run.
If I try something like
val path = getClass.getResource("h2-1.3.161.jar").getPath
in my test it does not work.
Any idea how to find a file inside a jar without be this jar part of your classpath?
Regards.
Using the solution of astrograph I manage to get this route
java -cp //file:/D:/Users/nb38tv/workspace/f2e-core/f2e-mock/f2e-test-framework/target/f2e-test-framework-1.8.3-SNAPSHOT.jar!/h2/sakila-h2-master/h2-1.3.161.jar -ifExists -tcp -web -tcpAllowOthers
But java complain since cannot find the jar.
If I remove the ! from the path I receive this error
Unrecognized option: -ifExists
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Any idea?
Regards
Is the file a .class file?
Can you open the file in your test?
Can you instantiate a class from that jar file?
To get to the location of a class you can use the following method:
System.out.println(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
It depends on what you mean by "find" the file.
If you mean "detect if the class file exists", then you can use the Java Zip file handling routines. Inside your code, open the Jar file as a Zip file.
If you mean "use the class file" within your program, and for some reason you cannot put it on your class file (perhaps it doesn't exist in that location at startup time) then you need to use an additional class loader which will look for the file after the file is present. To do this, I recommend you reuse at URLClassLoader, even if your file is local to the disk, just use a file:/// URL.
I have trained a model using maltparser version 1.8.1, and I can parse text just fine, but I tried to do it from a non-local directory,
java -Xmx6G -jar /Users/me/maltparser-1.8.1/maltparser-1.8.1.jar -c /Users/me/maltparser-1.8.1/configFile -m parse
and I get the following error:
Couldn't find the MaltParser configuration file: /Users/me/maltparser-1.8.1/configFile.mco
However, the file definitely exists. I copied the text from the error:
$ ls /Users/me/maltparser-1.8.1/configFile.mco
/Users/me/maltparser-1.8.1/configFile.mco
The only way that I can get it to work is if I run java in the directory where the configuration file is. Is there something special that I have to do to use a full path for the configuration file?
It would be possible, I suppose, to cd in a shell script to make this work, but I still want to understand why doing it this way doesn't work.
I was facing the same problem, but then I looked into the source code and found out that you need to provide the relative path of the configuration file from the place you are executing the java command. This is because the path where you have executed the program is prepended to the path of the config file you provide as argument, unless you have mentioned the working directory as argument(in this case put the config in the working directory).
I'm trying to run my Java program using a batch file, I'm able to run it properly. However, when I run the batch file after inserting the code to read a properties file from the Java program, I'm getting the following error.
Can't find bundle for base name app1, locale en_US
Actually i have a conf folder under which I have this properties folder, then I came to know that I need to keep this conf folder in the class path. But I have actually added it as class folder using Eclipse. However, I'm getting the same error. Please let me know what exactly I need to do for running the Java program using batch file. Using Eclipse I'm able to run the properly.
Thanks,
Balaji.
From the comments your batch script:
#echo off
java -Djava.ext.dirs=lib -classpath ./bin com.myapp.app1.demoprogram
pause
Notice how the /conf/ directory is not listed on the classpath. The easiest way to get it there is to just add it to the -classpath argument being passed to the JVM. Something closer to:
#echo off
java -Djava.ext.dirs=lib -classpath ./bin;./conf com.myapp.app1.demoprogram
pause
This is assuming that /conf/ is in the same directory as bin. You might have to do some tweaking to get the setup to work for you application, but the root problem is that while you added the /conf/ folder to the project classpath in eclipse, you need to do the same thing for the batch script so the JVM can find it
Got a little problem loading a file! (Not a image just really a file like .txt and stuff)
It loads fine in Netbeans with
File myfile = new File("a/b/myfile.abc");
The problem is the compiled jar gets a exception and doesn't find the file. I need it as a file, not as a Stream or something, that's the problem and I have already tried everything that came into my mind to load it.
I would like to load it externally (not from inside the jar) and the problem is it doesn't seem possible to get a working setup with getRessource(AsStream).
EDIT:
Ok so i let it print the absolute path when it was compiled and when it was not compiled.
Non compiled path:
C:\Users\USERNAME\Documents\NetBeansProjects\ProjectName\a\b\myfile.abc <-- Correct Path
Compiled path:
C:\Users\USERNAME\a\b\myfile.abc <-- Not Correct Path
Can anybody tell me how to fix this?
EDIT²:
If I navigate to the correct folder with cmd (cd etc.) and start the jar after doing so the folder is getting loaded from the correct directory. Can someone tell me what I need to change?
EDIT³:
When not starting with cmd it seems to search for the folder in Windows/system32 :O
When you construct a file with
new File("abc.txt")
the abc.txt file is supposed to be in the current directory. The current directory is the directory from which the java command is launched to execute your application. So, if you're in c:\foo\bar and execute java -cp d:\java\app\MyApplication.jar MyApplication, it will look for the file c:\foo\bar.
The location of the jar of the application is irrelevant, and doesn't have any impat on where the file is looked up. The current directory is what matters.
At the moment, in my java program, I am accessing a file that is in the project folder. When I'm loading the file its path is "./src/package/package/file.txt". When I build the program into an executable it dosen't work.
I would prefer the files to be outside of the .jar, but in the same folder, how would I got about this?
Samishal
You can use a relative path if they are in the same folder, such as ./file.txt. That should carry over even with a compiled JAR.
Otherwise, if you're going to be using the same machine and are confident of the placement of the files, you could use an absolute path, however I don't recommend it.
You can use Class.getResourceAsStream to get the InputStream. It works for jar package too. It is not possible to access the file in jar file using File API directly.
InputStream ins = Class.getResourceAsStream("/gigadot/exp/resource.properties");
More details at http://blog.gigadot.net/2010/10/loading-classpath-resources.html
Because the relative path is relative to where your command prompt is when you execute the program, not from where the program lives.
You somehow need to tell the program where to find resources. Most people use a .sh/.bat script to figure out where the script itself lives, and either pass -D flags or set the classpath based on that location.
as a note, I $0 gives you the script as it was run on the command line in linux (it could be relative or absolute), and you can use dirname from there to find it's directory, and alter the java command line.
in windows %~dp0 gives you the directory of the batch script which was run, and you can use that to form your java command line.