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
Related
I've been working on a Java program for a company over the summer and it is nearing completion. Being something of a novice programmer, I've never produced a program for use on other systems using files I've created. My program reads and writes to Excel using Apache Poi, and currently the Excel file being used lies in a specific directory specified by the program. The program also uses some images that lie in directories specified by the code.
How could I make this program runnable on other systems? Would it be possible to have the program create an Excel document whenever it is "installed" on another system?
Currently I'm using Eclipse and the systems are windows 7.
You can pack your application in a .jar, actually a zip file with inside .class files and resource files. Those resource files, like your images, can be taken with getClass().getResource("path/x.png") or getResourceAsStream. The path is either package relative or absolute: "/abc/def.jpg".
These resources must be read-only (as they are inside the .jar). However you may store an Excel template as resource, and copy that to the user's directory.
System.getProperty("user.home")
Will give the user's directory where you may copy your resource to.
Path appWorkspace = Paths.get(System.getProperty("user.home"), "myapp");
Files.createDirectories(appWorkspace);
Path xlsx = appWorkspace.resolve("untitled.xlsx");
Files.copy(getClass().getResourceAsStream("/data/empty.xlsx"),
xlsx);
I have created a data driven framework in Selenium by using Java, Eclipse and an Excel file. I have exported the runnable jar file which executes perfectly in my system but it is not executing on others due to path problems. I wanted to know whether there is any specific way to use a data driven jar file in different systems by solving it's path problems? Or, do I have to do all installations and ensure all related resources are in the system on which I want to execute?
Use user.dir key for get project location using java.
Code:
System.out.println("Present Project Directory : "+ System.getProperty("user.dir"));
Use System.getProperty("user.dir") which require path and append the folder as per required in your project folder.
This is how you dynamically add the path. what ever the system it is. it will take the path of project directory dynamically
Hope it will help you :)
i hv created the .jar file by building my project in netbeans.
The .jar file exist in "dist/myproject.jar". But when i move it to
any other system will it find the paths specified for images etc in
project?
As i am giving the paths like
(C:/Users/Lucky/Documents/NetBeansProjects/CoverageAnalyzer/src/coverageanalyzer/icons/icon.png).
OR When i write path just to approach root directory as
(icons/icon.png), so then also?
Summary: What is the actual way that i can write/copy my .jar file to
any other system without spoiling the paths and program run correctly
on any other system. Thanks in advance.help will be appriciated
You can use one of the following approaches:
Use relative paths (relative to the executable file location) instead of absolute paths.
Use paths under a known absolute path (an installation folder, path from environment variable / configuration file / registry key, the user's directory (user.home) etc.)
Use resources embeded into your jar. See Class.getResource() and Class.getResourceAsStream()
Note that you should consider cross-platform (Windows/Linux/Mac) and resource hiding issues when selecting a suitable approach.
The solution is to put the auxiliary files as resources into to jar. The program must then load them from the class path instead of the file system.
Have a look at Class.getResourceAsStream.
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
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