I'm new to this - I have never tried to put an applet online and I'm a fairly new programmer.
I tried to put an applet onto a webpage; the first applet I tried didn't work (Hosted class on a google site file File Cabinet, didn't work because the FTP was ASCII and for classes that results in a magic number error)
So I found a place that can host my files and so that the FTP was BINARY (Which is required for an applet class to work). I made this change and the applet was fine, working fully in a browser, and I was happy.
So now I'm trying to get an applet that accesses text files (hosted in the same place as the class file) to work, but no matter what I try it can't access them.
From what I've read thus far, it seems like I have to create a signed applet so that I can access other files, but that means I have to make a jar file which I cannot do because this is an applet, no main method.
in short: I have an applet that tries to access other text files and it's not working (error in title)
I may have made a mistake somewhere, and if so - any help would be appreciated.
From what I've read thus far, it seems like I have to create a signed applet so that I can access other files,...
The problem is that a File created by applet code running on the client can never point to a location on the remote server. File objects just don't work that way.
This resource will need to be accessed by URL instead. To form the URL, use a relative path from the code base or document base (where the HTML is). Note that an applet can 'phone home' to get resources from its own code base or document baseāeven when sand-boxed.
Related
Kind of hard to explain in one line but my problem is essentially like this:
I made a java applet that I want to run on a web page that I packaged into a .jar file. I'm able to get the applet working fine using the <applet> tag but the problem is, if the user views the page source, they will see:
<applet archive="directory/program.jar">
Assuming .jar files can be easily opened and all the class files decompiled, all the user would have to do is go to www.url.com/directory/program.jar to download my .jar and they would have all my source code :(
So I'm wondering if there is either a way to protect my code/jar from being decompiled (other than obfuscation) or to use some kind of server-side script to feed the contents of the .jar directly to the browser from a server-side location not publically visible.
Any help is appreciated.
This is fundamentally impossible.
Java applets run the client.
Anything that runs on the client can be disassembled and modified by a sufficiently advanced user.
You should move your sensitive logic to the server and invoke it using HTTP requests ( and remember that the user can use Fiddler).
While you're at it, you should probably replace your applet with HTML and Javascript.
Other than obfuscation or encryption, no--one way or the other, the browser will have access to the jar.
You might be able to create an applet that loads more functionality at runtime.
There is no effective way to block access to the source code of any page; for the page to be readable by browsers and search engines, the source code has to be accessible, and therefore can be viewed and/or copied. That's just how the web works. HTML is sent as a text document and interpreted client-side.
Disabling the right-click is little more than an annoyance, and it works sporadically in alternative browsers. Even if you succeed, the View Source option in the menu is always present. The viewer could also use a download tool such as Wget, or even get the page from the Google cache without visiting your site at all.
Edit: Oops! I misunderstood your question. You should follow #SLaks advice and "move your sensitive logic to the server and invoke ot using HTTP requests ( and remember that the user can use Fiddler)."
While quantum mechanics do rule the universe, they have less of a grip on your code than you might suspect. You cannot both deploy code to the client browser and not deploy code to the client browser. You have the option of doing one or the other.
You can prevent direct browsing to your .jar file by locating it beneath the WEB-INF directory in your WAR file. This will also prevent <applet archive="directory/program.jar"> from working.
Once the jar is beneath the WEB-INF directory you will need something to feed the resource to the client browser; the Spring resources servlet is good for this (If you are using Java and Spring). I feel confident that other such tools exist. With the Sprint resours servlet, your would deploy your applet with something like this: <applet archive="resource/program.jar".
If you write your own resource distributor, you can add security to make it harder to get the jar file; perhaps add a header to your requests like IRGud: <user_id here> and fail any request that does not have that header (or acceptable contents in the header).
I have an application where javascript code is accessing java applet methods that write on local disk (just to note - this is not directly possibly, but there is a workaround for it that enables to do so). Applet is self-signed applet, so users have to allow it to run first.
If applet's codebase is set to "http://..." everything works fine, in FF, Chrome and Opera as well. However, I would like to put this applet on local file system. However, if I use codebase "file://..." (I also tried to not use codebase at all and write directly full jar path to archive) it does not work, applet does not load with Warning-Security pop-up that enable user to allow running the applets.
I only tested it in FF (3.6.1.4) so far, Java version is 1.6.0.21.
Are signed applets limited only for HTTP use? Or is there some workaround?
..is there some workaround?
Perhaps using the JNLP API services in an unsigned applet will work better. I have a demo. of the JNLP FileContents object.
For details on using the JNLP services in an embedded applet see the links to the 'next generation plug-in' in the applet information page.
Applets loaded from the local filesystem are allowed to write files only in, or below, the directory containing the applet. So, if the applet is in C:\MyProjects\MyAppletTest it will be able to read and write files in that directory and its subdirectories, but not in C:\MyProjects. You can override this behavior with a policy files as explained in Quick Tour of Controlling Applets
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"
In addition to what my title says, I am running into problems because their class file is linked as follows:
"var attributes =
{code:'xx/xxxx/xx/xx/xxx/xxx/xxxxx.class'
width:645,height:443,archive:'xxxxx.jar'}"
First, I naively copied the HTML code and it did show a Java Applet Object, but couldn't load it because it obviously didn't find the class. I tried many different addresses to see if I can download the class, but with no success. Does this mean the class can't be downloaded? I'm in the process of asking for their permission and see if we can get it directly from them.
I also thought of another way. Is it possible to embed their whole page as an iframe AND "crop" it so the iframe only displays the area where the Java Applet is located? If this is possible, it would be the best and easiest way.
You certainly can download the the .jar file - they have to be accessible so that browsers can load them. I'd guess you are trying to get the .class file, but it is within a .jar, so get the .jar instead.
I'm new to Java. I'm simply trying to build a .jar file of my applet so I can run it from my browser. This is what my directory structure looks like:
C:\java\pacman\src
contains all of the .java class files.
C:\java\pacman\assets
contains about 4-5 images and audio files.
If I try to use the following code:
Image someFile=getCodeBase().toString() + "file.png";
The result of getCodeBase() is
file:/C:/java/pacman/bin/
However the following code fails to load:
img=new ImgHelper(getCodeBase().toString() + "assets/");
ImageIO.read(new File(img.getPath("pacman.png")));
Moving my 'assets' folder to the 'bin' folder didn't fix this either. It tries loading:
file:/C:/java/pacman/bin/assets/pacman.png
saying:
Can't read input file!
But the url it gave opens fine if I paste it into run and hit enter:
So to avoid myself a lot of headache i commented out the code in my ImgHelper class and did this:
public ImgHelper(String dir)
{
//this.imgDir=dir;
imgDir="C:\\java\\pacman\\assets\\";
}
Which works perfectly. But I want to put this on a web server, and I have no idea how/what I should do to make all the images and sounds work. Any ideas?
Thanks...
Why not put it all in a JAR file and then call Class.getResourceAsStream?
A JAR file is better as it is a single HTTP connection rather than one HTTP connection per file. It is also much more flexible to use a Stream than a File.
getResourceAsStream will work when the files are not in a JAR as well, they need to be relative to the class file.
EDIT:
Another thing, the File method won't work if the applet is on a server as it will be trying to open the file from the local machine (I think, I haven't tried it) rather then from the server. Even if it tried to create a file path to the server that won't work.
I agree with tofubeer about the JAR, but if you want to put the image on your server, see the tutorial on Applet images here. The codebase will be whatever location your applet is on the server, and you can put images relative to that on the server as well. Use a media tracker along with the Applet.getImage() method to retrive the url. From the example:
my_gif = getImage(getDocumentBase(),"imageExample.gif");
There are two possible solutions that would work:
The images could be present outside the applet JAR. The applet could then be initialized with the location of the directory where the images are present. Once you have that information you could then load images from the server. The Sun Java tutorial provides an example usage of the applet parameter to pass the image source directory.
The applet class loader could be utilized to load the images from the applet's JAR, using the getResourceAsStream() method.
PS: It would be helpful if you referred to the section in the Java tutorials to load icons for your application. The same section discusses a lot of the points brought forth by TofuBeer and John.
EDIT : The usage of the File API is not recommended because it ends up reading off the local file system. That is unacceptable for most users on the internet.