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.
Related
I'm trying to read a text file located in src/main/resources/test/file.txt. I'm trying to get the path of the file using String path = getClass().getResource("/text/file.txt").getFile(); but when I try to read it I get a FileNotFoundException. I tried putting many different paths, all of which failed. How can I go about doing this?
The idea of putting something into the src/main/resources tree is that it will be copied into the JAR file that you build from your project. It will then be available to your application via the Class methods getResource(String) and getResourceAsStream(String) methods.
When you are running in your application in the development environment, it is certainly possible to use FileInputStream etcetera to access the resource. But this won't work in production. In production, the resources will then be inside your app's JAR file. FileInputStream cannot open a JAR file and its contents by name.
When you do this:
getClass().getResource("/text/file.txt");
you get a URL for the resource, which will look something like this:
jar:file:/path/to/your.jar!/text/file.txt"
It is not possible to turn that into a pathname the FileInputStream will understand. Whatever you try will give you a FileNotFoundException ... or something that is not the resource you want to read.
So what to do?
You have a few options, depending on your application's requirements.
You can use getResourceAsStream and use the resulting input stream directly.
You can copy the contents of getResourceAsStream to a temporary file, and then use the pathname of the temporary file.
You can create an application specific directory (e.g. in the user's home directory) and extract the file you need from the JAR into the directory. You might do this the first time the application runs.
You could open the JAR file as a JarFile and use that API to open an InputStream for the resource. But this assumes that that the resources are in a JAR ... and on some platforms (e.g. Windows) you may encounter problems with file locking. (And it would be a bad idea to attempt to update the resource in the JAR.)
Try giving complete path of the file from the disk.
C:\Users\MyUser\Desktop\file name with extension
I made a small Java program for academic purposes, its main focus is to read some .txt files and present the information to the user. These files are present in the resources folder, under the src folder.
The program runs as intended when launched from Eclipse.
Using the Launch4j app I was able to successfully create an exe which runs fine and does what's intended, up until I try to read the .txt files I have in the resources folder, which appears not to be able to reach.
I'm guessing that when I launch the exe the run time path would change to where the exe was created, so I created the program in a desktop folder and specified this path in the program, but that doesn't seem to solve the situation.
As an alternative, I moved the .txt files out of the program and once again created the exe in a desktop folder with said .txt files, linked the program to this path and once again it didn't work.
The command used to get the .txt files is:
Files.readAllLines(Paths.get(doc)).get(line)
And doc is simply the path to the intended .txt file.
It's worth noting that I have no previous experience in Java and throughout the development of the program I tried my best to use commands I'd fully understand and to keep it as simple as possible. I hope the solution can be along these lines! I'm very confident this must be a rookie mistake, but I can't seem to find the solution to this specific problem anywhere.
The paths to files in Eclipse are different than the paths to files in an .exe or JAR file.
I will let this other user explain it because I am lazy :p
Rather than trying to address the resource as a File just ask the
ClassLoader to return an InputStream for the resource instead via
getResourceAsStream:
InputStream in = getClass().getResourceAsStream("/file.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
As long as the file.txt resource is available on the classpath then
this approach will work the same way regardless of whether the
file.txt resource is in a classes/ directory or inside a jar.
The URI is not hierarchical occurs because the URI for a resource
within a jar file is going to look something like this:
file:/example.jar!/file.txt. You cannot read the entries within a jar
(a zip file) like it was a plain old File.
This is explained well by the answers to:
How do I read a resource file from a Java jar file?
Java Jar file: use resource errors: URI is not hierarchical
The original post is here, all credit to its author.
Fixing your URL should let you read from that file when you are using the .exe.
EDITED FOR CORRECTION. Thanks #VGR (see comments) for correcting my mistake.
I have a Java project,exported as a JAR file (Desktop Application) which generates a HTML file as output. The output html file, needs to read one image file, as the page's logo. The JAR application will be in say X folder. The target html file will be placed dynamically anywhere. How do I make the html,residing in someother location, access the image, inside the JAR file.
In short, how do I determine the path for the below code, for the above scenario.
java.net.URL url = getClass().getResource("image.jpg");
fw.write("<tr><td><b>"+csname+"</b></td><td> <img src = "+url.toString()+"'>/td></tr>");
works fine, when i run in eclipse. But not when exported as JAR
The resultant html file,in some other folder has the code
<img src="rsrc:com/demo/dirapitoword/image.jpg">
You just need to read the image as a stream from the classpath, e.g.:
InputStream in = getClass().getResourceAsStream("image.jpg");
and write the stream out as a file to a known place on disk. There are lots of ways to do this, if you're using Java 7 or above, try:
File out = new File("image.jpg");
Files.copy(in, out.toPath());
Then, your src attribute can use the relative location you chose to display this image in the HTML, without having to worry about Jar compatibility in the browser / client.
You can find that the jar: URI Scheme exist for .zip containers as described in the Wikipedia here http://en.wikipedia.org/wiki/URI_scheme
The format is:
jar:<url>!/[<entry>]
BUT it is not supported by all browsers (only Firefox actually) as described in this article: https://code.google.com/p/browsersec/wiki/Part1
Even in that case, I think it is not very nice that the output .html can be elsewhere but contains an absolute path to your resources.
I suggest to use the data: URI scheme which allows to embed the image within the HTML page.
There is a question that covers this procedure: How to display Base64 images in HTML?
And in one of the answers, there is an interesting fiddle sample here: http://jsfiddle.net/hpP45/
I am trying to use a jar file which itself is a web application in another web project. In my jar which i have created using eclipse's export to jar functionality, I have stored a csv file in a folder. To use relative paths in the code in the jar I access it using
MyClass.class.getResource(ApplicationConstants.ALIASESFILE).getPath();
and this works fine when I deploy (glassfish) and use the project as a separate application. But when I am using the same from within another project, it gives a path as shown below
D:\javaProjects\AutomodeGS_Prachi\lib\internal\RESTWSGS.jar!\aliases\aliases.csv
I am getting a file notfound exception.What could be wrong?
The getResource() method is returning a "jar:" URL. The path component of that URL is not a normal filesystem pathname, and can't be opened directly using Java's file classes.
The simple way to do this is to use Class.getResourceAsStream(...) to open the stream. If you need an "identifier" for the JAR entry, use Class.getResource(...), but then open the stream using URL.openStream().
This works fine from glassfish may be because glassfish has exploded jar on file system so that your csv file is acutually a file to the file system,
if you try to read it from another project it fails because the jar containing your file is in classpath that is fine, but the csv file is under jar file and it is no longer a File
You can read it as Stream
InputStream is = MyClass.class.getResourceAsStream(ApplicationConstants.ALIASESFILE);
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.