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/
Related
I want to host my html asset into LMS, here LMS only accept and run the SCORM Package.
How can i convert an html file (exporting from my app using jasper report) to a scorm package?
Any help?
For single pages, in fact the best approach I found was to do it mannually: based on: https://www.ispringsolutions.com/blog/how-to-convert-html-to-scorm to adapt it, just ignore the res folder and point to the index on: <resource identifier="resource_1" type="webcontent" adlcp:scormtype="sco" href="index.html">
Download a SCORM example: https://21w98o3yqgi738kmv7xrf9lj-wpengine.netdna-ssl.com/wp-content/assets/golf_examples/PIFS/ContentPackagingSingleSCO_SCORM12.zip
extract it;
in imsmanifest.xml change the title to your title;
List all the resources that your HTML page includes;
Zip the course root folder (with the manifest file).
On this page you will find software that you can use to create SCORM compatible packages. If you have created a simple project, you can unpack the resulting ZIP file and edit the content. Mostly you will find an index.html there. I know, that sounds a bit awkward at first.
https://docs.moodle.org/35/en/Creating_SCORM_Content
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.
this title may sound confusing so I'll explain my problem.
I am converting my java application to an applet, and it works fine in Eclipse and when I make a html file with tags. But when I upload my applet to google sites and execute it via a html file, the program itself loads fine, but it tries to load its images from my own hard disk instead of from the JAR file. How can I change that?
HTML:
<applet
archive="https://sites.google.com/site/projectteagame/tea.jar"
code="ymte.core.Loader"
width=300
height=300> </applet>
Edit:
I keep all my files in the jar, for speed and ease. My code to obtain the images is:
img = ImageIO.read(Loader.class.getResourceAsStream("/res/object/ball.bmp"));
This works in my IDE, but not in the online use
You can provide your code to tell us how you obtain the image, but I suppose it is not bundled in the jar. A way to do that is to put the jar in your packages and obtain it like this:
Class.getResourceAsStream("foo");
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)
This is kinda gimmicky, however. A preferred way would be to put your image into a directory in your project that is outside your sources, something like images or resources and then bundle it in the jar of the applet. Use your favorite build tool (ant, maven, ivy) to include the image in the jar. Directory structure can be like this:
project
-src
-resources
-<yourImage>
Then you would access your image as a URL or File using a relative path. If you provide more code, maybe you can receive further help.
EDIT:
SInce your resource is in your applet, you can put it in one of your packages. Then, further in your code you can do
Class.getResourceAsStream
and that is how you can get your image, since it is guaranteed to be in your classpath.
Basically, if you have a package structure like this:
project src
com.foo.bar
com.foo.bar.stuff
com.foo.bar.resources
-yourImage
-otherStuff
-ResourceGetter.java
You can do the following in your ResourceGetter
Class.getResourceAsStream("yourImage");
Which will give you a handle to your image and you can load it as you would do normally.
I want to add a picture to my GUI program created using Eclipse and MyEclipse (for GUI visual design) from the resource pictures I pasted earlier in the project.
I managed to load pictures that lies just beside the .JAR file using
image = ImageIO.read(new File("imageFile.jpg"));
But I want to use the image from my resources "src" folder directly , so that the .JAR file is a standalone file yet loads pictures nicely.
I tried to make it
image = ImageIO.read(new File("src/ldtlogo3.jpg"));
I use this method when exporting the .JAR file
Java: export to an .jar file in eclipse
Use the overloaded ImageIO.read method taking an InputStream as a parameter, and use MyClass.class.getResourceAsStream() to get this input stream. getResourceAsStream loads a resource from the classpath (and thus from the JAR of your application). Its api doc will tell you which path it expects.
Note that the src directory is used to hold your Java source files. The jar doesn't contain it. It contains the .class files, in a hierarchy which directly maps the package hierarchy. Eclipse will automatically "compile" the image file by copying to the output directory, along with the .class files.
Can anybody tell me how do i give absolute path of the img tag's src attribute?
The following doesn't work
<img alt="NO IMAGE" src="/home/administrator/tiger-info0[1].gif"/>
I am working On Ubuntu and i am very sure that image exists on this path.
This is probably happening because the image is located outside the web server's document root.
Your web server will not be able to serve anything from outside the document root. One possible workaround is to use a scripting language that has access to the file system, and route the images through the script. For example, you may want to check out the following implementation in php:
Serving Images Outside Document Root Via PHP
You can also create a symbolic link of /home/administrator/ into the document root:
ln -s /www/yoursite /home/administrator
hmm why don't you copy the image to your web directory and give it the relative path? you server (apache?) may not be able to access the file to serve the browser.
if you are making a local html page you can use that path but if you are creating a website you have to use the absolute path to the document root. And make sure the image path is correct (use firebug)
Give your path correctly with domain or use ../ or ./ is for to represent correct relative path.
You cannot access files that are not in your document root. Get Java application server to not delete your folder. You can probably do this by having one folder into which your users can upload files, and add that folder to your project. You can let users create subfolders inside that main folder, and since the main folder is a part of your project, cleaning the build will not automatically delete it or its subfolders.