Get absolute URL of jfx path - java

This line
URL contexturl = Thread.currentThread().getContextClassLoader().getResource("ClassLoader/Test/someFile.xml");
gives me the following contexturl.toString() string for the resource file someFile.xml:
jfx://ClassLoader/Test/someFile.xml
What does this mean and how can I obtain the absolute file system path of the resource?
The problem is also that contexturl.getPath() returns null.
The someFile.xml file is packaged in some jar file can be loaded as expected. However, I also want to load another file which is in the same folder of the jar but not packaged in the jar, so its external and therefore I need the file system path.
Any hints how to retrieve this path?

Related

How to read driverpath using relative path mentioned in config.properties file

I have config.properties file in src/main/resources folder having a property as "driverFilePath=C:\\VSP\\workspace\\Drivers\\IEDriverServer_Win32_3.5.0\\IEDriverServer.exe".
Here I'm currently reading it by mentioning full path, now I want to read only through relative path as "Drivers\\IEDriverServer_Win32_3.5.0\\IEDriverServer.exe" instead, as the drivers are also located in svn repo.
I'm using the code as
File file = new File(driverFilePath); to get the path from local.Please suggest a way so that I can mention only relative path like "Drivers/IEDriverServer_Win32_3.5.0/IEDriverServer.exe" which will match both my local and also the svn location.Hence I won't be making any change in my code.

Java - getting image from file path

Hey is it possible to get an image from file without using the full file location in java? My code below will only work with the full file path:
Icon icon = new ImageIcon("/Users/MyMac/Documents/Project/Software/Project/src/UI/Images/default_pic.png");
Is it possible to use the file path as such?:
Icon icon = new ImageIcon("/src/UI/Images/default_pic.png");
"/src/UI/Images/default_pic.png" is an absolute path, so it will look for a src directory in the root directory, then a UI subdirectory in it, etc. Not what you want.
You can use a relative path such as "src/UI/Images/default_pic.png" (notice it doesn't start with a "/"), but as its name says, it is relative to the current directory. So it will work if your current directory is /Users/MyMac/Documents/Project/Software/Project (or any directory that contains the file in the same subpath), otherwise it won't.
Finally, another way is to access the file through the classpath. Considering that the project could be packed in a jar file, the image file might not be a separate file on the disk, but you can still get a URL or InputStream to access it. Search for getResource and getResourceAsStream in Class and ClassLoader.

How to specify relative file path in Java file so that it can still work after the file is put in jar file?

Suppose I have a Java class that needs to access a file with absolute path
/home/gem/projects/bar/resources/test.csv:
package com.example
class Foo {
String filePath = ????? // path to test.csv
String lines = FileInputStream(new File(filePath).readAllLines();
}
Where the path to Foo.java is /home/gem/projects/bar/src/com/example.
Of course I cannot specify absolute path to the resource file. This is because jar file will be distributed as library for any clients to use in their own environments.
Assume the resource file like test.csv is always in the same path relative to project root. When a jar is created containing Foo.class, this jar also contains test.csv in the same relative path ( relative to project root).
What is the way to specify relative path that would work no matter where the project bar is moved to? Also how can I create a jar file (which can be in any location) so that the path to the resource file test.csv would still be correct.
To keep things simple, I have used invalid Java API ( readAllLines() which reads all the lines and return a string containing entire file content. Also not using try/catch).
Assume csv file can be read as well as written to.
I hope this makes it clear now.
Put the test.csv file into the src folder and use this:
Foo.class.getResourceAsStream("/test.csv")
To get an InputStream for the file. This will work wherever the project is moved, including packaged as a JAR file.
Example:
ProjectX\src\Test.java
ProjectX\resources\config.properties
If you have the above structure and you want to use your config.properties file, this is how you do it:
InputStream input = new FileInputStream("./resources/config.projects");
In this example you don't have to worry about packaging your source into jar file. You can still modify your resources folder anytime.
Use getResource(), as shown here.

Upload file to directory on server using Java and JSP - can't get path right

On my vps, I want to upload a file to the Logos directory.
The directory structure is as follows on my vps -
/home/webadmin/domain.com/html/Logos
When a file is uploaded through my jsp page, that file is renamed, and then I want to put it into the Logos directory.... but I can't seem to get the path right in my servlet code.
Snippet of servlet code -
String upload_directory="/Logos/"; // path to the upload folder
File savedFile = new File(upload_directory,BusinessName+"_Logo."+fileExtension);
//.....
//file saved to directory
//.....
I've tried many variations, but still fail. What is the proper way to specify the path?
Edited
The problem with using getServletContext() is that it returns the path to the directory where Tomcat and my webapp is...whereas I want to reach the directory where my html and image files are - under the root directory of the vps. How do I specify that path?
String server_path = getServletContext().getRealPath("/"); // get server path.
//server_path = /opt/tomcat6/webapps/domain.com/
String upload_directory = "Logos/"; // get path to the upload folder.
String complete_path = server_path + upload_directory; // get the complete path to the upload folder.
//complete_path = /opt/tomcat6/webapps/domain.com/Logos/
File savedFile = new File(complete_path,"NewLogo.jpg");
//savedFile = /opt/tomcat6/webapps/domain.com/Logos/NewLogo.jpg
It's a common practice to make the path for storage configurable - either via some application.properties file, or if you don't have such a properties file - as a context-param in web.xml. There you configure the path to be the absolute path, like:
configuredUploadDir=/home/webadmin/domain.com/html/Logos
Obtain that value in your code (depending on how you stored it), and have:
File uploadDir = new File(configuredUploadDir);
Note: make sure you have the permissions to read and write the target directory.
You can use following code in any jsp or servlet.
1) String serverPath= getServletContext().getRealPath("/");
This will give you full path of the server from root directory to your web application directory.
For me its: "D:\local\tomcat-6.0.29\webapps\myapp" when I sysout from myapp application.
Once you got the whole real path for the server system as above you can get the path relative to your directory. So if I have some data file in myapp\data - I can get it appending \data\filename to the serverPath which we got earlier.
This will work in all situation even you have multiple servers installed on the same system.
2) You can get server home from system properties using
System.getProperty("TOMCAT_HOME")
and then can use this absolute path in your program
3) To pass absolute directory path to any servlet using <init-param>
Hope this will work for you.
Well, the problem is: the File constructor doesn't create the file, only prepares to for the creation, then, after you construct a file instance you must invoke the method createNewFile(), and thats all.
The path "/Logos/" will attempt to create the file in the root of your system, which is not what you want. Look at the ServletContext.getRealPath() method.

Getting the absolute path of a file within a JAR within an EAR?

I have a J2EE app deployed as an EAR file, which in turn contains a JAR file for the business layer code (including some EJBs) and a WAR file for the web layer code. The EAR file is deployed to JBoss 3.2.5, which unpacks the EAR and WAR files, but not the JAR file (this is not the problem, it's just FYI).
One of the files within the JAR file is an MS Word template whose absolute path needs to be passed to some native MS Word code (using Jacob, FWIW).
The problem is that if I try to obtain the File like this (from within some code in the JAR file):
URL url = getClass().getResource("myTemplate.dot");
File file = new File(url.toURI()); // <= fails!
String absolutePath = file.getAbsolutePath();
// Pass the absolutePath to MS Word to be opened as a document
... then the java.io.File constructor throws the IllegalArgumentException "URI is not hierarchical". The URL and URI both have the same toString() output, namely:
jar:file:/G:/jboss/myapp/jboss/server/default/tmp/deploy/tmp29269myapp.ear-contents/myapp.jar!/my/package/myTemplate.dot
This much of the path is valid on the file system, but the rest is not (being internal to the JAR file):
G:/jboss/myapp/jboss/server/default/tmp/deploy/tmp29269myapp.ear-contents
What's the easiest way of getting the absolute path to this file?
My current solution is to copy the file to the server's temporary directory, then use the absolute path of the copy:
File tempDir = new File(System.getProperty("java.io.tmpdir"));
File temporaryFile = new File(tempDir, "templateCopy.dot");
InputStream templateStream = getClass().getResourceAsStream("myTemplate.dot");
IOUtils.copy(templateStream, new FileOutputStream(temporaryFile));
String absolutePath = temporaryFile.getAbsolutePath();
I'd prefer a solution that doesn't involve copying the file.
Unless the code or application you are passing the URI String to accepts a format that specifies a location within a jar/zip file, your solution of copying the file to a temporary location is probably the best one.
If these files are referenced often, you may want to cache the locations of the extract files and just verify their existance each time they are needed.
You should copy the contents to a temporary file (potentially with a cache), because trying to get to internal files of the application container is a dependency you want to avoid. There may not even be an extracted file at all (it can load from the JAR directly).

Categories

Resources