I'd like to pass a file path argument to my application in a relative form, e.g ~/test.conf or ../test.conf, but i can't get a proper full file path, though i've tried it with old java.io and new java.nio Files/Paths. Is there a general way to get a resolved file path without large amount of code? It would be fine for the solution to work only in unix envs like OsX or Debian.
Update
With a provided argument like ~/test.conf
in case of getAbsolutePath it returns a path with a prefixed current folder - /Users/currentUser/Projects/Personal/TestProject/~/text.conf. Canonical path returns the same.
The hard bit here is dealing with POSIX home directories, and sure you deal with ~otheruser/dir/test.conf too (if you want to do it properly).
Luckily that's covered in How to handle ~ in file paths.
TL;DR - use something like:
path.replace("~/", System.getProperty("user.home") + "/");
Once you've done that, and as others have commented, you can just use standard java.io methods (including getCanonicalPath()).
Related
I want to check if a file exists on resources and use this line:
getClass().getResource(resource.getResourceFullName()) != null
resource.getResourceFullName() is a method that returns just the file name without slashes like testevnvariables.yaml
I got that file did not exist. For your information, I use Windows and IntelliJ IDEA locally but prod is on Linux.
Then I tried to do this, but, again, the file does not exist:
getClass().getResource(File.separator + resource.getResourceFullName()) != null
and only when I added manually "/" I got "exists":
getClass().getResource("/" + resource.getResourceFullName()) != null
So my question is why it is happening? As far as I know, the default separator for Linux is slash, and for Windows is a backslash. File.separator returns a correct value for Windows - backslash but Java can't find it.
I don't want to add "/" manually and want to be independent of the system and use something like System.lineSeparator() but for files. How can I do it?
I also tried to use File.exists() but with the same result.
And why when I found a file, Idea shows me this path (with slashes like on Linux):
.../target/test-classes/testevnvariables.yaml
A resource is something on the class path, which can be considered a sort of JVM specific virtual file-system (it is comprised of all JARs and directories, etc that have been specified on the class path). The file separator for this virtual file system is exclusively /. So, just use / and you are good to go. You don't need to fiddle with File.separator or anything, as the / is already system independent.
As an aside, even on Windows, most filesystem APIs allow you to use / and \ interchangeably.
I developed a software in netbeans + Ubuntu and then converted the runnable .jar file of netbeans to .exe file using a converter software.
I used:
File f = new File("./dir/fileName");
which works fine in Ubuntu but it gives an error in Windows, because the directory pattern of both OSs are different.
Absolute paths should not be hardcoded. They should be read e.g. from a config file or user input.
Then you can use the NIO.2 File API to create your file paths: Paths.get(...) (java.io.File is a legacy API).
In your case it could be:
Path filePath = Paths.get("dir", "fileName");
I used: File f = new File("./dir/fileName") which works fine in Ubuntu but it gives error in Windows, bcz the directory pattern of both os are different.
It is presumably failing because that file doesn't exist at that path. Note that it is a relative path, so the problem could have been that the the path could not be resolved from the current directory ... because the current directory was not what the application was expecting.
In fact, it is perfectly fine to use forward slashes in pathnames in Java on Window. That's because at the OS level, Windows accepts both / and \ as path separators. (It doesn't work in the other direction though. UNIX, Linux and MacOS do not accept backslash as a pathname separator.)
However Puce's advice is mostly sound:
It is inadvisable to hard-code paths into your application. Put them into a config file.
Use the NIO2 Path and Paths APIs in preference to File. If you need to assemble paths from their component parts, these APIs offer clean ways to do it while hiding the details of path separators. The APIs are also more consistent than File, and give better diagnostics.
But: if you do need to get the pathname separator, File.separator is an acceptable way to get it. Calling FileSystem.getSeparator() may be better, but you will only see a difference if your application is using different FileSystem objects for different file systems with different separators.
You can use File.separator as you can see in api docs:
https://docs.oracle.com/javase/8/docs/api/java/io/File.html
How would I access a picture in a different folder in Java? I have a series of pictures and they change based on user input, which is what x is for.
picture.setIcon(new ImageIcon("\\resources\\icons\\pictures\\"+x+".png"));
The images are located (from the .class files) in resources/icons/pictures, but the above code doesn't work. The value of x isn't the problem since it works as it should. Am I calling the pictures the right way?
Am I calling the pictures the right way?
Probably not. If they are (embedded) application resources they will typically be in a Jar and unavailable via the String based constructor of the ImageIcon (which expects the String equates to a File path).
For an embedded resources, access them by URL.
URL urlToImg = this.getClass().
getResource("/resources/icons/pictures/"+x+".png");
picture.setIcon(new ImageIcon(urlToImg));
You should be using forward slashes instead of backslashes I believe.
new ImageIcon("/resources/icons/pictures/"+x+".png")
This is the standard Java cross-platform way of denoting resource file paths. It still works on Windows - the Java runtime library handles the transaltion for you.
The images are located (from the .class files) in resources/icons/pictures
That's a problem. The system isn't interested in where the class file is, but from where you invoke a program.
Specifying a resource folder via command line,
java -jar myJar.jar C:\\home\\of\\the\\images
or via a property
java -jar myJar.jar -DImageHome=/foo/bar/images
or from a properties file is most flexible.
If you like to put the images into the jar, use Andrews suggestion, getClass ().getRessource ("...");
Btw: I know for sure, that forward slashes are portable. Backslashes, afaik, aren't.
I have for example .pdf file (path to that file). How to open this file in default application (probably Acrobat Reader) from SWT application (for example on Button click) ?
You should be able to use:
Program.launch(file);
to open the file (using the default application or creator). From the javadoc:
Launches the operating system executable associated with the file or URL (http:// or https://). If the file is an executable then the executable is launched. Note that a Display must already exist to guarantee that this method returns an appropriate result.
Note that there are some peculiarities in Program.launch() (or at least there were, though these may have been fixed in more recent versions of the runtime.) I don't really remember the specifics of the bugs, but we do some checks to work around some issues:
If you're on a Unix platform, and you're specifying an absolute path, there may be trouble opening that file. We prefix absolute paths with /. - so that /tmp/foo would be translated to /./tmp/foo - although I don't really remember the specifics of this bug any more than that.
On Windows, if you're trying to open a UNC path - for example \\server\bar - you need to wrap the string in double-quotes. For example: Program.open("\"\\server\bar\"");
Try Desktop.open:
Desktop.getDesktop().open(file);
Maybe this can help to find a decision: we ran into PermGen space trouble upon call Desktop.open() - which is in AWTpackage - out of our SWT application.
So I would prefer Program.launch() over Desktop.open() in a SWT-environment.
I have created a Java application that loads some configurations from a file conf.properties which is placed in src/ folder.
When I run this application on Windows, it works perfectly. However when I try to run it on Linux, it throws this error:
java.io.FileNotFoundException: src/conf.properties (No such file or directory)
If you've packaged your application to a jar file, which in turn contains the properties file, you should use the method below. This is the standard way when distributing Java-programs.
URL pUrl = this.getClass().getResource("/path/in/jar/to/file.properties");
Properties p = new Properties();
p.load(pUrl.openStream());
The / in the path points to the root directory in the jar file.
Instead of
String PROP_FILENAME="src/conf.properties";
use
String PROP_FILENAME="src" + File.separator + "conf.properties";
Check the API for more detail: http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html
I would also check what your current working directory is if your path to that file is relative. You just need to make a File test = new File("."); and then print that files canonical path name.
If you are referencing any other locations like user.dir or something to that effect by using System.getProperty(), you'll want to at least verify that the directory you are using as the relative root is where you think it is.
Also, as Myles noted, check the slashes used as file path separators. Although you can always use the "/" and it works.
And if you are referencing the path absolutely, you'll have trouble going between one OS and another if you do something silly like hard-code the locations.
What you want to do is check out System.getProperties() and look for file.separator. The static File.pathSeprator will also get you there.
This will allow you to build a path that is native for whatever system you're running on.
(If indeed that is the problem. Sometimes I like to get the current directory just to make sure the directory I think I'm running in is the directory I'm really running in.)
Check your permissions. If you (or rather, the user that the Java process is running under) doesn't have appropriate permissions to read the file, for example, you would get this error message.
This is a typical Windows -> Linux migration problem. What does ls -l src/conf.properties show when run from a prompt?
Additionally, check capitalisation. Windows isn't case-sensitive, so if the file was actually called e.g. CONF.properties it would still be found, whereas the two would be considered different files on Linux.
You should check the working directory of your application. Perhaps it is not the one you assume and that's why 'src' directory is not present.
An easy check for this is to try the absolute path (only for debugging!).
I would check your slashes, windows often uses '\' vs linux's '/' for file paths.
EDIT: Since your path looks fine, maybe file permissions or executing path of the app is different?
check your slashes and colons
in my case i set my PS1 to following value
PS1='\n[\e[1;32m]$SYSNAME(\u)#[\e[1;33m]\w [\e[1;36m](\d \T) [!]\e[0m]\n\$ '
i am trying to read from the env .such as system.getenv
Java was throwing exception
java.lang.IllegalArgumentException: Malformed \uxxxx encoding
Try the double slash, after doing things in JBoss I often had to refactor my code to use the double slashes