This question already has answers here:
Changing the current working directory in Java?
(14 answers)
Closed 7 years ago.
I have an eclipse rcp application, which i am launching by setting the working directory in the arguments tab of the debug configuration.
Meanwhile i needed to change the current working directory upon application start.
I've tried the following options : System.setProperty("user.dir", this.strDestination);
But it doesn't work if we use relative file path, as it refers to the older working directory. Only solution that is working in this regard is using getAbsoluteFile or getAbsolutePath (which is not a feasible option as relative paths is used in a number of location).
Any approach in this regard is highly appreciated.
I'm pretty sure you can't modify the current process' working directory. Instead you can use the File(String, String) constructor which creates a new File instance from a parent pathname string and a child pathname string.
Is your this.strDestination variable an absolute path?
According to this answer, you should use an absolute path in System.setProperty for the "user.dir" changes to take effect.
Note: I didn't have enough rep to add a comment. Hence adding as an answer.
Related
so I did run into one very weird issue. The idea is simple: create temp dir, place some files in it and then try to access them. Now the problem is that calling File.createTempDir() or Files.createTempDirectory(prefix) creates new file inside AppData/Local/temp with shortened path, so the full path to folder looks something like C:/Users/FirstNam~1/AppData/Local/Temp/myFolder/myFile.txt instead of C:/Users/FirstName LastName/AppData/Local/Temp/myFolder.myFile.txt.
The difference is that generated path inside java contains FirstNam~1 instead of FistName SecondName. Java then throws exception File Not Found.
When I try to copy and paste shortened path into file explorer I get an error saying that file does not exist, but if I do change shortened path to full one then file opens and it works as intended.
Is there any way to fix it? Ether by forcing java to use full path names or enabling something in windows? I did Enable NTFS long paths policy, but it did not help.
This is happening when using java 8/11 and windows 10 running on VM, project is using AGP and gradle. Temp file is created inside groovy file that extends Plugin<Project>
Just when I lose hope and create a ticket, couple hours after that I find the answer. So, java has method Path.toRealPath() which solves this ~1 issue. After using this method paths no longer contain shortening and are correctly resolved.
EDIT: looks like java is doing everything correct and paths are actually valid, problem did come from library that I'm using and it's a bug with it.
This question already has answers here:
Reading a .txt file using Scanner class in Java
(11 answers)
Closed 3 years ago.
I have an assignment from my teacher i have make a serial program into a parallel with OMP.It's the Barnes-Hut one and its the first time i'm using netbeans.
I have a text file with someone numbers and i gotta import it into the project so it can use the values the text file has.How can i import the example.txt into netbeans?
I've tried this but it doesn't work
File myFile = new File("example.txt");
It also contains a scanner
public static void main(String[] args) {
// for reading from stdin
Scanner console = new Scanner(System.in);
Make sure your text file is in the classpath
You have a number of choices.
1. Relative Path
You can use a relative path. This means you take your current "working" location and look for the file from there, this is basically what you're doing now.
The problem with this is, the "working" location changes, and is based on the location from which the program is executed. Remember, the location of the class is not the same as the "working" location.
Netbeans "default" "working" location is usually the root directory of the project. There are ways you can configure Netbeans to use a specific "working" location, but you need to consider what happens when you're no longer running in Netbeans.
So a "simple" solution would be too drop the file into the Netbeans project directory. A slightly more advance solution would be to configure the project's "working" directory to a location containing the file
2. Absolute Path
This is when you use a fully qualified path from the a root location to the file (ie C:\some\place).
The problem with this is, rarely are this paths the same from one computer system to another and you need to remember to place the file into this location each time.
2.1 A "well known" location
A form of the absolute path solution is to place the file into a "well known" location, typically based on the OS.
For example, on Windows, you would typically use something like {user.home}\AppData\Remote\{name of your application}.
This would allow you to place the file in, what is essentially a "static" location and load it when ever you needed to.
But, this is probably over kill for your needs
3 Embedded resource
A more common solution is to "embed" the resource with your application. Essentially, this is placing the file within the applications class path, which allows the Java runtime to locate it.
In Netbeans, this means putting the file within the src directory, preferably in a subdirectory, like resources for example.
Then, when you want to read the file, you'd need to use Class#getResource(String) or Class#getResourceAsStream(String) to gain access to it.
So, based on the available information, and the example above, you might do something like...
try (InputStream is = YourClass.getResourceAsStream("/resources/example.txt")) {
// Read the file...
} catch (IOException exp) {
// Handle the exception
}
Just remember, this renders the resource "read-only" (at least for context of this simple example)
This question already has answers here:
How to read text file from classpath in Java?
(17 answers)
Closed 3 years ago.
I want to read the configuration file outside the jar, how should I do?
I tried classloader.getResourceAsStream("config.xml") and succeeded while I am running the code inside Intellij. Now I want to build the jars to a folder and place the config.xml under the same folder, not inside the jar, but the program fails to detect the config.xml.
Is there a graceful way of reading the config.xml instead of using File with relative path in the code, which doesn't work while debugging/running inside the IDE?
Yes, turn it into a system property, and provide it to anything running any Java process/application.
Let's say you have a config.xml file located inside /some/path/down/the/line/, then you can do: java --classpath ... -Dapp.config=/some/path/down/the/line/config.xml tld.domain.Application.
Then all you have to do in your Java code is to reference that name/path: final String configFile = System.getProperty("app.config");, and use any well-known routine to read it from there.
Basically, you have to make sure the file/path/location is provided somehow to the Java classpath.
This question already has answers here:
How to get the current working directory in Java?
(26 answers)
Closed 3 years ago.
Part of my (java) code needs to access a database. When opening a connection it checks if the actual database file exists (I use sqlite). I want to make my code portable, so I want to avoid hard coding the path of the database file. Is there anyway in java to get the path of the .java file? Because I know exactly where the database is from the .java file accessing it.
I've tried using current directory with File but it doesn't give me the path of the actual .java file. When I use android studio the current directory is different than when I simply use a terminal.
The best way to get the path information would be using Paths and Path class from the java.nio. You can input an absolute path or relative path to the Paths.get(String str) to get the Path.
To get the project directory, you can use:
Paths.get(System.getProperty(“user.dir”))
Paths.get(“”)
It will get the complete absolute path from where your application was initialized.
The Paths.get() method will return a Path Object, on which you can call toString() or toUri() to get the path. Hope this helps.
Javadocs - Paths
This question already has answers here:
How to add resources to classpath
(3 answers)
What is a classpath and how do I set it?
(10 answers)
Closed 5 years ago.
I'm on a new branch of a git project on which I've made my own config file called MY_NAME.conf which is located in
Users/myname/IdeasProject/projectName/src/main/resources/config.
However, I'm having some issues getting my branch to run. So, just to make sure things are smooth I set up a new environment and I've cloned into the master. This is all done at
Users/myproject
However, now when I try to run the project I get a message
java.io.IOException: resource not found on classpath:
config/MY_NAME.conf
Why is it looking for this file at all? And even if it is, why is it not finding MY_NAME.conf?
Consider setting the classpath through one of these recommended techniques. Without knowing the specifics of your runtime environment, there should be a mechanism provided to include src/main/resources in your java classpath.
I think the issue is that classpath doesnt include conf folder.
You can modify/set class path with: set CLASSPATH=path1;path2