Opening HTML files in SWT browser from .jar with hyperlinks working - java

I have a set of help files for my SWT application that I have open in-application using the Browser control. Navigation through the help files is done through hyperlinks of relative pathnames (i.e: <a href="aboutUs.htm">, so only one html file is actually opened by java code, helpHome.htm. I am opening this using String homeURL = this.getClass().getResource("/help/helpHome.htm").toString(); and browser.setURL(homeURL); This works beautifully when I'm just debugging it in Eclipse. Unfortunately, when I move the project into a .jar, the browser gives the standard "can't find this webpage" error. I've tried using the browser.setText(String); function as described in this link, which works for helpHome.htm, but when I click a hyperlink, it brings me to a blank page displaying the relative pathname. Is there a way to convince browser to open an html file from an executable jar using the setURL(String) method? If not, are there any suggested workarounds for me to achieve similar results?
Thanks in advance!!

You have to start an internal Server as explained here on any available port on your application start up and make your html files available as static resources to the server.
Then set the browser.setURL(homeURL). All the subsequent hyperlinks will now point to the server, which knows how to serve the requested resources.
The hyperlinks are resolved to File System Path (Ex. file://C:\workspace....) while you are running or debugging your application in eclipse and things were working fine then. But that is not the case when you run your app from a runnable jar.

Related

Possible to Run Normal Java File Via Browser?

I've coded a game in Eclipse (still working on it), is it possible to run it's jar file in browser so that anyone can play right from their browsers instead of downloading jar file?
I.e I upload it on my website so that anyone can play right from the link I provide them.
Consider using Java Web Start with JNLP. You can host a JNLP file on a web server somewhere, along with your jar, and users can use a desktop shortcut pointing to the link, or a browser to open your app. (Using a browser will require having a web page with an applet tag: the article mentions how to do this but doesn't separate the two.)

Deploy Applet on Apache server

I've written a Java applet game that I want to deploy on my Apache server. Code + resources are in a self signed .jar with accompanying HTML doc both in the root dir. When I run it on my local machine everything works fine. When I try to run on the server I get a class not found exception. index.html contains
<applet code=SpaceRaiderz.class
archive=spaceraiderz.jar
width=1024 height=768>
</applet>
The class not found is the class named above. Opened up the .jar and its contents are complete. File permissions set to 755. If I click the index.html on my local machine it loads up and runs perfectly. Ftp the same docs to the server and it doesn't work. Anyone had this problem? I found a similar question to this on here but none of the suggested solutions have worked in this case.
The applet (seen here) works (e.g. showing an asteroids style game featuring a pale green ship that appears suspiciously familiar + sound track) for me in Java 1.7.0_21 on Windows using FF. Refreshing the class cache in the console & then refresh the page might fix the problem for you.
If that fails, there are still more options for testing the applet:
Usually installing another browser (these things are often due to a problem in the particular browser/JRE combo.).
There is also the Appleteer applet testing environment. I can highly recommend it, Since I wrote it.
Go to an internet cafe or the local library to check an applet, if all else fails.

JSP Limit the directories of a File Open dialog

I am currently coding a program using the language JSP. I was wondering if/how you would restrict the directories a File Open dialogue box can browse, for instance:
The default directory is in C:/userFiles/username/ I want the user to be able to browse files in that /username/ directory, or directories within that directory, but not any directories above like C:/, or C:/userFiles. I have done some research, and I could not come across an answer. Is this possible with JSP, HTML, or Java and if so, how would I do it? I would prefer JSP or HTML.
Additional Info:
The dialogue box would browse files on a remote server, not on the user's computer.
The File Open dialog is on the client machine and is part of the operating system. You cannot change its behaviour.
In order to list files on a SERVER you would not use a File Open dialog, you'd be turning the list of files into HTML and serving that to the browser. Since you're the one writing the server-side code, you are ALREADY the one in control over which files make it into that list in the first place.
Saying you'd prefer "JSP or HTML" is a pretty good indicator that you don't fully understand the client-server relationship of the web.

Applet jar not being searched for files

For example purposes, let's say I have a series of Locations on a website and the urls are of the form /location/#/ where # is the id of the location I want to view. Since I'm using Django with Apache, all of my static content is in /media. Each Location page is trying to load a Java applet that allows for file uploads.
<applet
codebase="/media/java/"
code="com.elementit.JavaPowUpload.Manager"
archive="JavaPowUpload.jar, commons-logging-1.1.jar, commons-httpclient-3.1-rc1.jar, commons-codec-1.3.jar"
width="200"
height="100"
name="java-uploader"
id="id-java-uploader"
mayscript="true"
alt="JavaPowUpload by www.element-it.com"></applet>
All of the listed jar files are in /media/java/ and are found by the web server. The applet appears to load on the page without a problem but when looking at the network traffic during page load I see there are several errors. Basically the applet seems to be looking for files that are within the jar, say com.elementit.JavaPowUpload.Messages_en.class, but is asking the web server for them, which amounts to requesting /media/java/com/elementit/JavaPowUpload/Messages_en.class, which of course does not exist. Note that if I get rid of codebase and give the full path to each jar, I still have a similar problem where the request is then /location/#/com/elementit/JavaPowUpload/Messages_en.class. How do I set things up so that the jar file is searched rather than the filesystem?
See the codebase_lookup applet attribute.
Despite taking this code from another page on a different server, it appears there is a slight error in the applet's code attribute. Adding .class to the string fixed my problem, but I'm unsure why it works without it on the other host and page.
code="com.elementit.JavaPowUpload.Manager.class"

Can't open generated .mht file from Tomcat webserver

I'm generating a custom Word documents in my web application using the .mhtml format. Previously this was working fine. I assemble the document and write it to a directory then give a link to the file (with a .doc extension). Recently my template has changed to include embedded images. Now when I attempt to save and open a file from the web app, Word refuses to open it. It just shows a message "Cannot open file .." with the path. When you view the help it says something about lack of memory which is bogus.
When I open the exact same file from my localhost server it works fine. The two files (local and production) are identical. I've tried adding the site as a Trusted Site - no luck.
Any ideas?
Jeff
Sounds like the webserver does not annotate with a correct mimetype.
Is the downloaded file identical with the one present in the webserver? Do a byte-wise comparison.
Also try other browsers to see. IE has some quirks.
Solved issue by switching to plain HTML format with images as external links.

Categories

Resources