I'm having a little difficulty determining if the path in the code below is a relative path or an absolute path. Also, in this case, I'm trying to open an image I placed inside a folder called "img" which is inside my java project directory. Please don't mind the double backslash (\), I know these only works on Windows, the only thing I wanted to ask if this path relative or absolute.
ImageView img = new ImageView(new Image("file:img\\square.png"))
As you can find in the documentation here you can pass to the constructor any URL supported by the URL class, which is the case in your example.
If the passed string is not a valid URL, but a path instead, the Image is searched on the classpath in that case.
In your case, it is relative.
The file: URL scheme refers to a file on the client machine. There is no hostname in the file: scheme; you just provide the path of the file. So, the file on your local machine would be file:///~User/2ndFile.html.
Please read this answer for further information.
Related
I just came up with an error in Java (using Eclipse). I want to load an image from the resource folder into the application. Using the follwoing lines:
URL url = this.getClass().getClassLoader().getResource("/resources/images/icon.png");
BufferedImage i = ImageIO.read(url);
But this results in a java.lang.IllegalArgumentException: input == null! exception.
My folder structure is:
How can I access this image? Thank you a lot!
getResource() returns null if it can't find the resource on the classpath.
In order to use getResource() you need the resources to be on the classpath. The resources directory isn't on the classpath. In Eclipse, you could add the resources folder to the classpath. Or create a new package images under srcServer and move the icon out of resources and into srcServer\images along with your source code.
Another way would be to load the image using a File rather than loading it as a classpath resource.
I believe the reason why it doesn't find the resource is due to your syntax. getClass().getClassLoader().getResource() takes the input without the leading '/' and always starts at the root of the classpath. getClassLoader().getResource() is always an absolute path, whereas getClass().getResource() is a relative path.
Just use:
URL url = this.getClass().getResource("/images/icon.png");
Basically, I want to include my main JFrame's icon in the JAR file, so not to need to load it from an external location.
To achieve this, I searched about Java's resource system.
What I have done with Eclipse:
I have created a new folder named "res":
I have copied the files inside it using Windows' explorer:
I have made that folder a source folder:
I have written this code:
URL url = ClassLoader.getSystemResource("/res/icona20.ico");
But url is null.
What did I do wrong?
As mentioned you seem to have added res as source folder, so it is a root, not to name, like src.
URL url = ClassLoader.getSystemResource("icona20.ico");
Class loaders use an absolute (case-sensitive) path, without explicit leading slash /....
Relative paths with an obligatory leading slash for absolute paths:
URL url = Xyz.class.getResource("/icona20.ico");
And you might prefer .png instead of .ico as the latter format is not standard in Java SE.
(About common practices.) The build tool maven uses as nice standard the following source folders:
/src/main/java/
/src/main/resources/
/src/test/java/
/src/test/resources/
Your usage of res is reminiscent of MS Visual Studio ;).
The classloader will get the resources starting from each source folder you added to the classpath. Therefore, the URL should be the following:
URL url = ClassLoader.getSystemResource("icona20.ico");
I have a path that points to a file on disk, say: C:\folder\dir\dir2\file.txt. In the code, if an exception is thrown when working with this file, it will output the whole path. Ideally, it would be nice not to have the whole directory print out, rather something like ../../dir2/file.txt.
It seems like I should be able to that with the java.nio.file relativize method, I'm just not sure how.
Path file; // C:\folder\dir\di2\file.txt
file.relativize(file.getParent());
I'm approaching this the wrong way I'm sure, just not positive how to accomplish what I'd like.
Get the current working directory
Path pwd = Paths.get("").toAbsolutePath();
and then relativize the target Path
Path relative = pwd.relativize(target);
I have a java program which gets some properties from a xxx.properties file. For example the destination of a file my program works with.
How is it possible to give this file's place in the xxx.properties file with relative linking? I tried so many ways, but nothing worked. If I give the place of the file with an absolute URL it works just fine.
Example:
keyFileName=../res/MP00.pem <-- does not work.
keyFileName=/home/thomas/myprogram/src/main/webapp/WEB-INF/res/MP00.pem <-- does work.
The xxx.properties file is in /home/thomas/myprogram/src/main/webapp/WEB-INF/lib
I'm using an ubuntu based linux distribution, if that matters.
Any idea? Thanks in advance!
This has little to do with the fact that you load the URL from a properties file. Relative paths are always relative to some form of 'current location'. Loading the URL-String from a properties-file does not set that .properties' location as your 'current location'. Try setting the path relative to the program you run (which uses the URL-String), not the .properties-file.
I'm trying to create a file in my server. I have sent a image, and I want to create that Image in a folder of my server, but with relative path.
String filePath = "C:\\Users\\Administrador\\Desktop\\Proyecto\\clienteServidor\\Server\\folder\\image.jpg";
File imageFile = new File(filePath);
...
I'm doing with the absolute path.
Thanks
hard coding a directory is seldom good for coding. What happens if there is a typo in your code. Using a combination of ./ or ./*
or even using
new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
This is explained here.
It is doable but, as Dmitry said, it might not work on every server. SecurityManager class should be consulted if your webapp has the privilege to write to that folder. or you will get an exception.
One way to do it is via ServletContext:
URL webAppRoot = this.getServletConfig().getServletContext()
.getResource("/images/new-image.jpg");
This will point to your ${tomcat}/webapps/mywebapp/images/new-image.jpg.
Another way is via ProtectionDomain:
URL runningClassLocation = this.getClass().getProtectionDomain()
.getCodeSource().getLocation();
But this will most likely give you jar:file://...myapp.jar!/my/package/servlet.class.
After you have the URL you convert it to File and append any relative path to your image folder.
UPDATE:
I agree with Jim, and emphasize that doing it like this is just for academic purposes.
Java is not like PHP so you shouldn't have uploads folder inside your web application's folder. Usually this is done by enabling an administrator-level user to specify a file path to a folder reserved for your application's storage needs.