Java file access - java

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.

Related

Providing path relative to main class for classpaths when running from command line

I am trying to execute a compiled version of my program attachmentUploader.jar from the command line. This has a number of dependencies on various Java libraries, which I am planning to bundle up with the program and specify as part of the classpath. This package will be moved between environments, and will be executed using the Java runtime for that environment. Using absolute paths for my environment, the command looks like this:
"C:\Program Files\Java\jdk1.6.0_37bin\java" -cp "C:/Users/My Documents/jars/attachmentLoader.jar";"C:\Dev Tools\jxl.jar";"C:\Dev Tools\org.apache.commons.codec";"C:\More Dev Tools\java-json.jar" com.custom.test.postClient
I would like to amend all the classpath jar paths to be relative to where my main class is saved, but I read that relative paths in this situation would be relative to where the Java is executed from. In my case this could be anywhere, as it will depend on where this has been installed. Is there a straightforward way of getting round this?
Thanks all.
There are actually several ways:
Make your jar file an executable jar file. That is done by adding a manifest file to the jar specifying what the main class is, and where the jar dependencies are located, relative to the location of the main jar file. The command to execute the program would then be java -jar path\to\jarfile.jar
Use a wrapper executable or shell script, that uses the location of the script itself to compose the classpath. Such a script would depend on the system (Unix/Windows), but both platforms allow script files to know their own location, and thus to use that location as a basis for the location of jar files. An advantage of that technique is that it also allows passing system properties, memory options, etc. to the JVM.
I recommend you use a standard build tool such as gradle, which has an application plugin generating all those script files for you, for all platforms, and bundles the whole application into a zip or tar.gz file.

Set a Path for database file in java

I am developing a program that uses a sqlite file as a database. When I compile and test my program I haven't any problem:
Conection c = DriverManager.getConnection(
"jdbc:sqlite:/home/mehdi/my_database.sqite");
as you can see in above, the code shows a direct path that set to database file (this path is only in my system).
So it works fine, but my problem starts when I create an executable jar file of my program, if I create executable jar file and share it with other users, when they run the executable jar it doesn't work.
My first question: how do I set my database path for an executable jar file in my code?
My second question: is it possible for the database to be along side the executable jar? (and i can move my executable jar file with its database)
some_path/my_program_file.jar
some_path/my_database.sqite
The best option is externalizing the database path instead of hardcoding it, and letting the user choose where to put the actual sqlite file.
You could put the sqlite file in the same directory as the program archive, but please note that this does not really help in locating the file from your code, because relative paths are definitely resolved against the JVM (process) working directory, not the location of the JAR:
new File(".").getAbsolutePath()
There are ways to get the location of the JAR:
getClass().getProtectionDomain().getCodeSource().getLocation()
but they are deploy-dependent, so it's not a very robust strategy to rely on.
Most Java program are configured with external files put in well-known locations (like the current JVM directory or a .myapp/conf.properties in the user home), and individual properties can be overridden in the starting command line, as either system properties -Dkey=value or program arguments (there's a library to simplify this)
Java uses relative paths when you do not specify the absolute path. I'm not sure if it works for the DriverManager though. What this means is that if you call jdbc:sqlite:my_database.sqite it should look for my_database.sqite in the folder the jar is executed from. So if the user calls java -jar my_program_file.jar from the folder where the database is it will work. If the user calls the jar from another location it won't. To fix that, you have to grab the path where the jar is located, like so:
return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
This was showcased in another SO post.

java command line argument to make jar find files in two directories?

I have a jar file which I do not have the source code but want to use.
The jar file prompts for a file to read and generates an output file using a combinatin of the input file and a number of 'helper' files it uses for data. It works perfecty fine if run from its expected home directory, but I'm trying to write a script which will allow running the jar from anywhere.
The problem is that if I try running the jar file from anywhere other then its home directories it fails to find the support files it needs to properly generate its data.
If I run the file from its expected home directory I have to give the full address of the input file or it won't find it. I would prefer to be able to give just the relative path and Java know to look at whatever directory the person calling my script is in.
Is there a way I can have a bash script pass a command line argument to Java that would ensure that this jar looks at both of the relevant directories (directory of the helper files and the current dir of the person calling the script) when trying to resolve a relative file path? Something like the -classpath argument?
With the --classpath (or -cp) you can tell your Java program where it should take the dependency classes. So, probably if you do like in your files directory
$JAVA_HOME/bin/java -cp '.:/path/to/the/original/program' My.class myfile.txt
then it will wind the program, and find your files as well.
UPDATE
If it doesn't work, you can try to force the file loading some other way. The Javadoc says:
By default the classes in the java.io package always
resolve relative pathnames against the current user directory. This
directory is named by the system property user.dir, and
is typically the directory in which the Java virtual machine was
invoked.
So, you can try running the program from the original directory this way:
$JAVA_HOME/bin/java -Duser.dir=/path/to/the/files/directory My.class myfile.txt
UPDATE2:
As I wrote in a comment, you can try symlinks. Execute the following commands in the original directory:
ln -s /path/to/the/files/directory datafiles
$JAVA_HOME/bin/java My.class datafiles/myfile.txt
Sorry - ignore. I missed the first line of your question.
You could pass the two paths as an argument to the jar file - then append the path location at runtime. Many ways to do that, here is one:
java -DdirectoryA="/somewhere" -DdirectoryB="/elsewhere" -jar program.jar
and in your code
String pathA = System.getProperty("directoryA");

How to get location of runnable .jar File when run from GNOME

I am developing a small Java application using Swing, that is supposed to run on Windows/Linux and MacOS.
Eventually it will ship as a runnable jar with some config files in the same folder. To load them I need the path to the folder the jar is stored in within the program.
There are already a couple of threads like this one or this one.
My problem is, that all the solutions discussed there work fine, when I run the program from within eclipse or call the runnable jar from a terminal like so:
java -jar /path/to/jar/jarfile.jar
However when I click on the jar file in Cinnamon or Gnome (which is what most of the users will know to do), I do not get the correct paths. (MacOS users report the same issue)
Here is what I've tried so far and what the output is when run via double click (all those display the desired path when run from eclipse or a terminal):
ClassLoader.getSystemClassLoader().getResource(".").getPath()
Output: file:/usr/lib/jvm/java-6-openjdk-common/jre/lib/ext/pulse-java.jar!/
SomeClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Output: ./
System.getProperty("user.dir");
Output: /home/myusername
Is there any other way to do it or am I doing something wrong when exporting the jar? Any help would be appreciated!
Cheers
Nick
Make it simple, and use a startup script (.bat/.sh file) to run your application. This startup script will get the path of its own location in the filesystem, and pass it as an argument or system property to the Java application. This has the additional advantage of being able to pass other arguments, like the size of the heap, etc.
On windows, %~dp0 is the path of the directory containing the executed bat file. On Unix, you can use $(dirname $0).
You could store all config files as resources in a jar, and copy them to files in home.dir + ".AppName/".
Painful as it is, the Preferences API, Preferences.systemNodeForPackage, seems the wisest alternative, if there is little structured config data. There is an inputStream method for import; your initial config template could be a resource in the jar.
Just get the class path using System.getProperty("java.class.path") and scan it for your ".jar" name. Note that path separators are OS dependent (File.pathSeparator)

Java: Loading resources from the file system

My Project Setup
I have the following project setup:
\program.jar
\images\logo.png
In my code, I reference the image with the relative URL "images/logo.png".
Problem
If I run this program with the following command while in the directory:
c:\projects\program_dir\bin\>java -jar program.jar
Then everything works and Java is able to locate the image.
Now, my problem is, that I need to be able to run the program from a different directory.
c:\>java -jar c:\projects\program_dir\bin\program.jar
The program is executed, but now all the relative URLs no longer work.
What I need
How do I calculate the execution home of the program.jar file, so that I can change my relative URLs into absolute URLs?
What I would do, if possible, is package your images in with your Jar.
That way you don't have to worry about where your Jar is launched from.
You would then need to load images similar to the following:
InputStream stream = this.getClass().getClassLoader().
getResourceAsStream("/images/logo.png");
You should really include your resources in your JAR.
Failing that, you can parse the absolute path out of the Main class resource. Assuming your Main class is actually called "Main":
Main.class.getResource("Main.class").getPath();
Resources have to be in the class path(s). You may try one of these:
Placing the images into the program.jar.
Like most of the programs, they have a APP_HOME directory, e.g. MAVEN_HOME, register that and refer to your files as File instead - $APP_HOME/the-file.ext (with System.getenv()).
As Nelson mentioned, package them with your JAR (just copy them alongside your .class files), then you can find them via a simple this.getClass().getClassLoder().getResource("images/foo.png").
You need to add a parameter which describes your class path. Should look something like
java -cp c:\projects\program_dir\bin -jar program.jar
this will put your jar and image on the class path and it will load successfully.

Categories