Having a lot of trouble deploying a java applet - java

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.

Related

Minecraft Server Resource Pack Manager Application

fellow developers.
Let's imagine the following scenario: A group of friends is playing Minecraft on a server. That server has a resource pack associated (file server.properties, field resource-pack). That resource pack is a ZIP file hosted by Dropbox, and the respective Dropbox share link is what goes on field resource-pack.
All good, for now. Resource pack loads flawlessly.
However, those friends need to update the resource pack with their custom textures every now and then. I'd like to automate that process in the best way possible. To provide a custom texture, one needs 2 files: a .png file and a .properties file. Those are fairly easy to make. Then, those files need to be put in the following directory (Dropbox):
/ResourcePack.zip/assets/minecraft/optifine/cit/
The problem here is that the Dropbox API can't upload to existing ZIP files, and the resource pack has to be a ZIP file, int order for Minecraft to recognize it. Also, I don't want to download the ZIP file, extract, put the files inside, compress and upload again, because it's quite a large file and that would take some time.
My Java application looks like this:
What the application does now, is accept dragged files and send them to the Dropbox path:
/ResourcePack/assets/minecraft/optifine/cit/
But not:
/ResourcePack.zip/assets/minecraft/optifine/cit/
I was told the Dropbox API can't handle uploads to ZIP files, and I also can't find anything on the documentation here.
Can anyone think of a solution, other than downloading the full resource pack, and uploading it back everytime someone wants to add custom textures?
Thank you.
EDIT:
So, as Adriaan suggested, I can simply use a direct link to the unzipped directory structure of the resource pack.
The problem is now another:
When a player joins the server, and downloads the resource pack for the first time ever, everything is fine. However, it gets cached in his local machine, in the following path:
/.minecraft/server-resource-packs/
So, when the resource pack is updated with new custom textures, minecraft will just load the cached resource pack and ignore any updates present in Dropbox. This results in the player not seeing the new custom textures.
I've learned about the resource-pack-sha1 field in server.properties. It looks just like what I need, but I can't quite understand how to use it.
Thanks in advance.
You can store the unzipped directory structure. DropBox allows downloading a directory as a zip file.
You can force dropbox to do a direct download by changing the dl=0 at the end of the URL to dl=1 see this help page
If the remark on the page you linked is still true "This is not yet used to verify the integrity of the resource pack, but improves the effectiveness and reliability of caching." then you could just enter a random new value into resource-pack-sha1 to trigger all clients to download the new pack.
If you need the actual digest then you could follow this mini tutorial
to generate an SHA1 for your server.properties:
visit this website: http://onlinemd5.com/
Upload your resourcepack
copy the SHA1 and paste it into your server.properties for the resource-pack-sha1 option
all done!
If you don't want to upload your resource pack to someone else's server you could download a tool to determine the digest locally.
Note: DropBox offers a content hash via their API, but it's not an SHA1 over the complete file. Rather they split the file in 4 MB blocks and determine the SHA256 for each, concatenate them and SHA256 the result again. If there's a way to change the hashing algorithm used by Minecraft this could be used.
EDIT: Is it an option to rename the resource pack directory on DropBox when you change its contents? This will circumvent the caching and hashing issue. Clients will just need to change the resource pack URL accordingly.

Using OutputFileStream("test.abc") in an Applet on a server is creating an new file in the folder of the browser

First, sorry for my bad english.
I'm not a pro in java and never programmed an applet before. So I'm trying to program a little game for my website. In this game I want a picture as background so i tried to load it with:
Image im = Toolkit.getDefaultToolkit().getImage(path);
That throws a FileNotFoundException, so I tried to creat a new file with FileOutputStream("test.abc"), to look where it will be created. I did this and it was created in my local firefox folder.
So how can I load the picture from a folder on the server?
I did this and it was created in my local firefox folder.
Of course it did. Why are you surprised? FileOutputStream doesn't speak HTTP to your server. It talks to a local file system.
So how can I load the picture from a folder on the server?
getCodeBase()+"/back.jpg" should work if back.jpg is in the directory referred to by getCodeBase().
Since it is a BG image it might as well be an embedded resource. To 'embed' it, include it in one of the Jars of the applet, then access it according to the link above.
You might also consider deploying a frame (as opposed to an applet) from a link using Java Web Start. That provides lower development/maintenance time and a much better user experience.

Access Control Exception - Access Denied

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.

How to embed a Java Applet from another website (can't link their class file and jar)

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.

Load jar file locally

Let's say I have a jar file and I want users to load it locally from the Internet... to speed up the loading I say... how would I go about doing that?
Like when you access it from http://mydomain.com... the jar file is from my documents/game/test.jar
Look up the URLClassLoader class, I think it will do what you want.
Here is a good description:
http://java.sun.com/developer/technicalArticles/Networking/classloaders/
EDIT: Forget it, Unsigned Applets cannot create ClassLoaders.
For an Applet you will need to download everything at once.
It is exactly the purpose of JNLP files (also called "Java Web Start"). Or I have misunderstood your problem...
You can find the official documentation on Oracle website : http://download.oracle.com/javase/1.5.0/docs/guide/javaws/developersguide/syntax.html
An example famous application using this system : SweetHome3D
I'm not sure if I understand your question, but it sounds like you want Java Web Start.
http://download.oracle.com/javase/tutorial/deployment/webstart/index.html
You would create a jnlp file, and put a link to it on your website. Java will take care of downloading the file and starting it. The .jar file would be stored in a cache, and it shouldn't need to be redownloaded unless the cache is cleared or you replace your old .jar file with a new one.

Categories

Resources