I have a small java app that updates files in a set location.
the app reads from a UpdateFiles folder, moves the files in this folder to the appropriate folder. the app works. what i want to know is how should i set my path if i want the app to always try and look for the UpdateFiles folder in same location that the app is? for example if the app is executed from desktop i want it to look for the UpdateFiles folder in desktop, same for any other location. The app runs on windows and AIX/linux enviroments
thank you / kind regards
You can access the system property that contains the current working directory:
String currentDirectory = System.getProperty("user.dir");
You can determine the location of the jar file, but this is not the same as the current working directory (the directory you get if you don't specify a path)
However the location of the jar is rarely a good place to store files and you don't want to mix released programs with files as this make upgrading your software much more complex.
I suggest using the user's home directory System.getProperties("user.home"); or a sub-directory This doesn't change if you upgrade you software is should always be writable.
You have to start your app from the dir where UpdateFiles is located, if necessary you can create a bat file (for Windows) and run it from any location
cd c:\path_to_UpdateFiles
java MyApp
do something similar in Linux
Related
I am working on a Java (JavaFX) desktop app. I am converting it to exe with launch4j tool and then later creating a setup package using Inno Setup Compiler (default installation path set to C:\Program Files (x86))
The application itself don't need any admin privileges and runs fine when I copy the exe on desktop or run it on drives other than C. (Also works fine if I run it in local appdata folder)
But I need to place it in Program Files (x86) directory.
The app doesn't open if I install it in that directory so I had to create a manifest file for launch4j so that it asks admin access each time it opens. It works fine that way but admin access is asked everytime in this case.
I need to make this application run without asking admin access each time.
If there is any solution, please guide me through it.
Any help will be appreciated.
Ok so I kind of figured that out. I guess the jar was having trouble reading from system directories and I had to do some read/write operations on a config file. So I changed the config path to:
String path = Controller.class.getProtectionDomain().getCodeSource().getLocation().getPath() + "\\Data\\";
Now it creates a folder named "Data" inside jar file and read/write from there. I don't know if this is a bad practice or not but it seems to work pretty good for me.
Thanks a lot to #user31601 who gave me some hint that helped me figure this solution out.
I've got a Java program which stores/recalls data from a load of .txt files located in my C drive. Currently, the file path for these is hard-coded into my Java program.
I'm now looking to distribute this software and am wondering how can I make it so that the user can specify a file location during installation and then I can point my program to this location without having hard-coded it.
This is pretty standard functionality but I've struggled to find how to achieve it. Can I use relative file paths, i.e. have the .exe for the java program in the same folder as the .txt files, or do I have to use something like Windows registry paths?
Many thanks for your help.
Cheers,
Robin
Use the system property "user.home" as the root of your path. Most of the stuff installed these days use this system property as the parent dir and a predefined folder. These saves a lot of trouble. Combine that with the File.separator char to avoid further portability issues
I'm working on a project where I open a file, overwrite it, and save it as a new file. However, I'm having some difficulties with accessing the template file.
Right now, I believe my program is referencing the file using the path from my computer, however, if I were to export this program to a different computer, it probably couldn't find the file.
I have the file stored in a source folder for the project in eclipse. Is there a correct way to reference the file so that it will be able to be found on any computer?
I've attached an image on how my program is now referencing the file.
If the file is in the project folder, you don't need to specify a path at all. Simply using the filename "file.txt" can suffice where you would have normally put a path.
One alternative can be if you are using Tomcat server for deployment , then place this file in web-apps location of tomcat.
Accessing will be done using below code snippet
String filePath = System.getProperty("catalina.base") + "/web-apps";
This filePath will work on all computers because we set catalina base in all machines installing tomcat.
I have a Java desktop application that I have written.
During the execution I create folders and files at the default path name defined in the system.
Java.io.files clearly states: 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.
In addition, I am using IzPack to enable installation and shortcuts creation.
When I'm running my application on my XP computer, after the installation I get a desktop shortcut, and the mentioned files and folders creation are at the location that Izpack installed the Jar. which is the expected behavior.
But when I test this out on a Vista machine, the folders and files are created on the desktop! even though the Jar is at the correct location (c:\program files.. etc).
I want those files to be created at the same folder the Jar is in, and most certainly not at the desktop.
Can anyone give me any insights on what is going on here?
It's because in Vista/Seven, writing to the Program Files folder requires administrative interference, so JVM looks for the next writable location as a fallback: the Desktop (or the User Documents directory). You can easily determine the User home directory in a unified manner on all OSs, though, which is way better than just letting the JVM pick a -- hopefully -- reasonable location.
Since this is a known bug for JVM on Windows, if that doesn't help, the fallback is to check the System Environment variable USERPROFILE which should point at the correct user home folder:
String userHome = System.getenv("USERPROFILE");
I have a NetBeans RCP application that's currently working on Windows and I'm trying to make Linux compatible. The application creates folders and files and modify files as well.
It works fine on Windows without any modification but on Ubuntu it fails creating folders during start up. I know it's a permission issue.
What are my options?
Can the application itself assign the permissions it needs like by running a script using ProcessBuilder?
Thanks in advance!
It all depends on who you are when running the process on Ubuntu, and the path of the folders that you're trying to create. Does this user have permissions to create the folders in that directory? What sort of data are you writing out to disk? Can you use a platform neutral mechanism thats user oriented, like Java Preferences or perhaps:
System.getProperty("user.home")
-or-
System.getProperty("java.io.tmpdir")?
You either need to create required folders as part of a setup process or restrict your IO to folders you have access to (the users home and the temp folder). Notice that on Linux there are standard locations where many folders should be placed and that administrators will frown upon applications that do not follow these standards.
Can you tell what files/folders you need for what purpose?
Looks like the cause of the problem is the difference in path delimiter between Windows and Linux. On linux you should use normal slashes. The error mentions the path:
/home/javier\marauroa.trace.db
As the \ is not a path delimiter but the escape character it is trying to create a file in the folder /home where it does not have permissions.
The path should be:
/home/javier/marauroa.trace.db
You might want to consider putting your apps files in a subfolder called .yourappname so then it would become
/home/javier/.yourappname/marauroa.trace.db
This is what many unix applications do and hide it in normal file listings. To get the path seperator for the system your application is running on you can use the following static field:
java.io.File.seperator