This question header isn't the best, but I am trying to create a directory from the person's computer in java, then when a button is clicked, to navigate to that created directory and create a file. I just need to get the path then create the directory. Here is a sample:
File theDir = new File("Users/" + System.getProperty("user.name") + "/InfoSaveFiles");
if (!theDir.exists()) {
if (theDir.mkdirs()) {
}
}
The only problem is that when I run this, (from a compiled jar file or from my IDE), it just creates a directory Users/(name)/InfoSaveFiles instead of navigating to that particular path and creating the directory InfoSaveFiles as I would like.
How can I make it go to the specified path, then create the directory?
If you want to are using that file path, it will look for Users/(name)/InfoSaveFiles in the present working directory, in case of IDE, the root of the project file. So, if you add a slash / before it, like '/Users/(name)/InfoSaveFiles`, then it will look for the hierarchy from the root of the current heirarchy. In case of windows, from the drive you're running the program.
For example, if you set the file path with slash before it and you're running the program in the E:// drive, then the program will look for Users/(name)/InfoSaveFiles in that drive, and will create the hierarchy if not exists.
So, if you want to do that in specific drive, you need to mention that too at the front of that file path.
For Linux, starting with / indicates the root directory. So you can achieve it in linux giving the current file path you've mentioned with an added / in the front. It will look for that path from the root directory then.
Hope that answers your question.
Related
I would like to write JSon files in my Java Application, it works perfectly in my IDE but as soon as I move the directory, the system fails to write the file. I eventually found the problem : The line System.getProperty("user.dir") returns a bad path.
The files are located in C:\wamp\www\myProject during development. Here is my code at the moment :
String url = System.getProperty("user.dir") + "\\src\\json\\Crc.json";
//Returns "C:\wamp\www\myProject\src\json\Crc.json"
After moving my project to C:\Users\myUser\Desktop :
String url = System.getProperty("user.dir") + "\\src\\json\\Crc.json";
//Returns "C:\Users\myUser\src\json\Crc.json"
I would like to have a way to find where my project directory is on the computer at anytime. Do you guys have a solution ?
The C:\wamp\www\myProject folder is just a random place on your hard disk.
Java supports 3 generic places to look for resources:
the current working directory. this is where your command prompt is and what System.getProperty("user.dir") returns, but you cannot rely on that beeing somehow related to any cretain place in the file system, especially not related to the project structure on your own system.
You should only use that if your program has a command line interface and looks for some default file name to work with.
the user home This is what you get when calling System.getProperty("user.home"). On Unix this resoves to $HOME and on Windows to %USERPROFILE%.
This is the best place for files changed at runtime or holding user specific content.
the own code location. Resources in the same package as your class are accessed with getClass().getResource("filenameWithoutPath") But usually you place resources in a special folder in the application root and access it like this: getClass().getResource("/relative/path/from/src/root/filenameWithoutPath").
In your IDE this special folder should be Project/src/main/resources (according to the maven Standard Directory Layout
This is appropriate for some global configurations that you change when creating the delivery package.
I made the following code to put in pictures to my java program:
private String ICET = "." + File.seperator + "Bilder/ICET.jpg"
//some code
label.setIcon(new ImageIcon(ICET));
//some code
It does, that the label has an IconImage and it works in every Workspace with a folder "Bilder", which is situated in the same folder as the program.
It works as a jar-file too, but if I convert the jar-file to an exe using launch4j, the program does not show the pictures.
use
new ImageIcon(getClass().getResource("path"));
path -> if is same folder put only name image : "ICET.jpg"
Most likely, the working directory set when started as an exe differs from when it is started as a jar-file, and thus your program looks at the wrong place for your "Bilder" folder.
You could try this out by printing the absolute path for it, e.g. like this:
System.out.println(new File(".").getAbsolutePath());
To fix the issue, you might try to set the chdir-option in the launch4j-config to . (a single dot). According to the docs:
<chdir>
Optional. Change current directory to an arbitrary path
relative to the executable. If you omit this property or leave it
blank it will have no effect. Setting it to . will change the current
dir to the same directory as the executable. .. will change it to the
parent directory, and so on.
But in the end, you should load the images from the class path, and not just from a folder. If you read them from the class path, they should hopefully be integrated into the exe, otherwise they are not and might get missing when users copy to exe to a new location, making your program no longer functioning.
I have a program that reads numbers from a .txt file. My problem is where to place this file, or how to reference it with a relative path to make this file accessible without using the absolute path.
When you try to open a file, it takes your current working path.
For example this working tree:
Project
|->src
| |-->MyClass.java
| |-->MyFile1.txt
|->res
|->files
|-->MyFile2.txt
You can use new File("MyFile1.txt"); for MyFile1.
or
new File("./res/files/MyFile2.txt"); for MyFile2.
You need to start your path from src. "src/your/path/file.txt". See my answer here
If you have a multi-project setup, it may not be obvious what the "root" directory is for a relative path. In that case, open the Terminal tab in your IntelliJ and see what directory it lands in. That is the "root" directory. Your relative path should start there.
Background
I'm creating an upload file functionality. I want to save the file in a given folder ("/uploadFiles"). I am currently testing in Windows but will run it in Linux.
I know I can get the current working TOMCAT directory. Is there a way to get the root system directory of TOMCAT directory? Say if the current directory is "D:\apache\sample_web_project" it will get "D:\" then I can append the "D:\uploadFiles" then if its in Linux it will get "/etc/apache/sample_web_project" it will get "/etc"
You shouldn't do that.
You should run Tomcat as a user, and then use its home directory as a base to store your files. Ie:
final Path downloadPath = Paths.get(System.getProperty("user.home"), "uploads");
Files.createDirectory(downloadPath);
Try this:
Path root = Paths.get(System.getProperty("user.dir")).getFileSystem()
.getRootDirectories().iterator().next();
On ubuntu it returns /
I have a simple java class in my web application in which i have written the below code but its not working
File test= new File("/templates/xmdForModel.xsd");
templates folder is inside the root folder of the application.
the location of the file is ----> application-root/package/test.java
location of the file is --------> application-root/testRoot/template/xmdForModel.xsd
Error
Failed to read schema document 'file:/templates/xmdForModel.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not .
If you want to look up the file name for files inside of your web application, you can use ServletContext#getRealPath.
However, I would recommend loading your resources using the classloader with Class#getResourceAsStream. This way, it even works if the file does not really exist as a file (for example only inside of a jar).
If this is a file that a user is supposed to edit (or that you write to), I would place it outside of the web application, and then specify an absolute path (for example "/etc/myapp/conf/xmd.xsd") with a configurable prefix.
Path in the File constructor can be absolute or relative. When you start the path with '/' (in a linux based os), it is going to consider that path as an absolute path and create a file/folder in the root of your file structure (not in the root of the project). It will be like specifying c:\templates in windows machine.
Try removing the first slash and running your program. Removing first slash will make the part relative from your .java file. So you can use ../ to switch to parent folder.
java: application-root/package/test.java
file: application-root/testRoot/template/xmdForModel.xsd
So from your java file you will need to change directory to application root folder and then select template folder. Like following.
File x = new File("../testRoot/template/xmdForModel.xsd");
src:http://docs.oracle.com/javase/tutorial/essential/io/path.html
/home/sally/statusReport is an absolute path. All of the information
needed to locate the file is contained in the path string.
A relative path needs to be combined with another path in order to
access a file. For example, joe/foo is a relative path. Without more
information, a program cannot reliably locate the joe/foo directory in
the file system.