I'm working with an applet that loads a resource file .properties for translation purposes, I am packing the applet in a jar file that also include the resource file inside. Already in the website, the applet works by loading the resource file from the jar file, but what I like to do is make the applet to load this file not from the jar but from an external path that is some where in the server, in this way the file can be edited without having to repackage the applet every time a change is neaded. The version of java I'm working is 1.7.0_01
URL url = new URL(getDocumentBase(), "../properties.prop");
Alternately..
URL url = new URL(getCodeBase(), "properties.prop");
See URL & Applet docs for the details of the constructor and methods.
Related
I need to get the path of a key file that i have placed in the root folder of my spring application. Everything works as expected when i run it locally. But when i deploy the application to the server i get a FileNotFoundException.
File file = new File("testfile.key");
String path = file.getAbsolutePath();
I have tried placing the file in the resource folder as well.
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
File file = new File(classLoader.getResource("testfile.key").getFile());
Just need to pass the file path to another method (3rd party library) which will read the content.
Any help would be much appreciated.
This should be a resource, placed in the resource folder (if using maven).
You can access it using
this.getClass().getResource("testfile.key");
The root for accessing files can change between environments but the root for resources is a directory that's specified during compilation. For maven driven projects this is:
src/main/resources
I have done a project with a simple Java Rest service and Ajax calls.
Unfortanely if i set the path to the json file as something general (fileName.json), it will not open my file.
If i set a complete path like C:\Users\Username\workspace\RestApplication\fileName.json, it works but when i will submit the project for review, it wont have the same path on my teachers computer.
The file currently resides in the main folder of the project. How can i make a general path that will work on whatever computer opens the project?
Thanks!
you should put the file you want to load in the classpath.
The class path is the path that the Java runtime environment searches for classes and other resource files.
Put your file inside your src or resources folder (I don't know your project structure)
and try to load it with:
InputStream is = TestResource.class.getResourceAsStream("/fileName.json");
or put the file under WEB-INF
#Context ServletContext servletContext;
InputStream is = servletContext.getResourceAsStream("/WEB-INF/fileName.json");
Although numerous different options you most likely want to refer to it on the classpath. See https://en.wikipedia.org/wiki/Classpath_(Java)
I'm trying to create a URL to access a local file like so:
URL myURL = new URL(getCodeBase(), "somefile.txt");
But it throws a NullPointerException when it attempts getCodeBase(). I'm fairly certain that the reason behind this is because the class file that this code belongs to is not an applet. Is there any way I can get the code base without using an applet? I just want to access a local file without having to put the actual directory in (because when others run the application the directory path will obviously not be the same).
I would use the following to be relative to the working directory
URL myURL = new URL("file:somefile.txt");
or
URL myURL = new URL("file", "", "somefile.txt");
or
File file = new File("somefile.txt");
You don't need to get the code base.
If the file resides on your classpath (this includes the path where your classes are deployed), you can access vía the ClassLoader method getSystemResource.
URL myURL = ClassLoader.getSystemResource("somefile.txt");
If somefile.txt is read-only, put it in a Jar that is on the run-time class-path of the application. Access it using:
URL urlToText = this.getClass().getResource("/path/to/somefile.txt");
If it is read/write:
Check a known sub-directory of user.home for the file.
If not there, put it there (extracting it from a Jar).
Read/write to the file with known path.
See How to create a folder in Java posting, which asked very similar question.
As Tomas Narros said above, the proper way to do this is to use the ClassLoader to locate resource files in the Classpath. The path you pass to the ClassLoader is relative to the classpath that was set when you started the Java app.
If you browse the above link, you'll see some sample code showing how to resolve the path to a file in your classpath.
My java database applet program read the file from their current directory like
FileInputStream fstream = new FileInputStream("details.txt");
When I run through appletviewer, it works but through browser, it doesn't showing any output.
error :
Error: details.txt (The system cannot find the file specified)
I put this file in same directory.
My applet tag is :
<applet code="hack.database.MyApplet.class" archive="MyApplet.jar, ojdbc14.jar" height="800" width="1000">
</applet>
Naturally you can't use a FileInputStream for this, FileInputStream is for reading files, and you can't access the local file system in an unsigned applet. Your resources are available over the net, not as files. If your applet is signed, the code you've quoted will look for the "details.txt" file in the user's current working directory in their file system, not necessarily the directory containing the class file.
You can load resources from within the jar the applet class is in using Class#getResource to get a URL you can open, or using Class#getResourceAsStream to do it all in one. So for instance, this code within an instance method within an applet will open an InputStream to the "details.txt" file in the same directory in the jar as the applet class file:
InputStream is = getClass().getResourceAsStream("details.txt");
I know that works for resources within the jar. Whether it works for other resources on the same codebase I couldn't say, I always bundle everything into the jar. See also this related question (and its answers).
So two steps: Put the file in the jar, and use the code above to retrieve its contents.
You have to use java.net.URL and java.net.URLConnection class method to obtain InputStream. Unsigned Applets cannot access client resources such as the local filesystem. For more information read - What Applets Can and Cannot Do.
Im trying to get the file path of a document that is packaged as a resource in a jar file so that i can display it in a swing application. The way I have it now works when I run it from eclipse but if I export it to a runnable jar file I can't access the the documents that are packaged in the jar file. How can I get the file path of the document when its inside the jar file?
Here is the line of code showing how I am trying to access the document:
File document = new File(getClass().getResource("/resources/documents/document.pdf").getPath());
The only kind of "file path" that exists for something inside a JAR file is the path relative to the root of the JAR. But in your case it seems that you know it already (it's "/resources/documents/document.pdf"). Files inside a JAR file have no path that you can use to access them directly as they don't exist within the real file system. You need to use either getResource() or getResourceAsStream() to access them. I don't remember right now which classes are used for images in Swing, but look closely at those classes - they should have overloaded methods that accept something like InputStream or URL instead of file path.