Skip to main content
gamedev.net
🔒 Locked

[java] multiple files in a Java applet

Started by EGD Eric Dec 13, 2005 at 6:42 PM 10 replies 1.5k views
Original Post
EGD Eric
EGD Eric
I'm having a little bit of trouble with this...my code works (most of the time) when I press the "run" button in Eclipse, but I want to see if my applet works with a web browser. I've done this before (using a web browser), when I only had one file to work with, but now I have multiple files. I looked something up on putting multiple files in an applet, and found something that looked like the following (in principle).
Quote:
Sample Thread applet

Sample Thread applet

All my applet is right now is a ball bouncing around the edges of the applet. There's 3 files: SpaceSoccer.class, Vector2f.class, and Ball.class. (hope you're not confused by the big bold heading inside my "code", its just something that happens when you paste HTML tags into a forum post I guess) But when I open the HTML file using a web browser, all I get is a "frozen" game. One with a ball that simply does not move. Is there something that particularly stands out about my HTML tags that you might see as the source of my problem?
Son of Cain
Son of Cain
Regardless of what is the problem, I suggest you pack your files into a JAR file. And then sign it.

I never seen the "archive" attribute of the APPLET tag refer to .class files, only for .jar files - but that could be really possible, I don't know. All I know is that you'll surely want to make a JAR for your applet, for the source files and the resources your applet uses.

Take a look at Ant targets to automate the building process of your application, making it easier for you to build the jar, sign it, and deploy it, at a single button click ;)

Son Of Cain
a.k.a javabeats at yahoo.ca
EGD Eric
EGD Eric
Ant targets?
Son of Cain
Son of Cain
Never heard of ant? If you're using Netbeans or Eclipse, I'm sure you have dealed with it before, you just haven't noticed. Ant is a tool to automate the building process (and many, many other tasks) of your application.

Son Of Cain
a.k.a javabeats at yahoo.ca
CaptainJester
CaptainJester
You don't have to sign the JAR file unless you are trying to do something that an Applet normally can't do.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
Son of Cain
Son of Cain
Yes, yes, thanks Capn'

Whenever I toy with applets, I'm always trying to reach out their security sandbox :p
a.k.a javabeats at yahoo.ca
EGD Eric
EGD Eric
I've tried a bunch of things in Eclipse to try to build a proper JAR file, one that will hold all my classes to be included in an HTML tag, but I've had no luck. I tried a lot of stuff that involved that "ant" symbol, but no luck yet... Can someone tell me step-by-step, how do I build a JAR file to hold my classes in so that I can run my multi-file program in a web-browser?
Son of Cain
Son of Cain
I do not use Eclipse, but I'm sure its standard building process already outputs a .jar file for you. Just copy and paste that to your web app dir and reference the .jar in the "archive" attribute

Son Of Cain
a.k.a javabeats at yahoo.ca
Glass_Knife
Glass_Knife
I searched the web for applet tag stuff, and everything I found only uses *.jar files in the archive tag. Yes, you can put more than one thing in the archive tag by using the ',' but they sould all be jar files.

Another thing I've noticed. I do a lot of applet code, and if I try to open the applet using the Eclipse 3.1 web browser that opens within the IDE, it never works. That is an Eclipse bug.

So jar your stuff up, and don't view from within eclipse.
EGD Eric
EGD Eric
Nope, Eclispe doesn't do that automatically. I followed your link, Glass Knife, and I found out how to Jar stuff using DOS commands. I did that, I jared up all of my .class files save my most important class (the one with run, init, that implements runable, etc..), and stuck in all my graphics and audio files in there too.

I put it into an HTML file like so:

WIDTH= 800 HEIGHT=600>


But when I run this using internet exlporer 5.0, the applet won't load properly, and I get this error in the status bar:

exception: java.security.AccessControlException: access denied (java.io.filePermission Graphics\Ship_Blue.gif read)

(Ship_Blue.gif is one of my graphics BTW, and yes, it loads fine when I run this program using eclipse)

I looked around on google to try to find an answer to this problem, but all I've found so far is information on protecting files . It tells me to go into my java home directory, find the file in lib\security called java.security, and type in something like:

    grant codeBase "http://h1.com/-" {        permission java.io.FilePermission "c:\\temp\\myfile", "read";    };


I Tried putting that at the bottom of the file, Except I put:
grant codeBase "C:\Documents and Settings\My Name\workspace\Test"{ permission java.io.FilePermission "C:\Documents and Settings\My Name\workspace\Test", "read";};


Yeah, I know, that is rather redundant that I put the same directory twice, but I didn't know what else to put as the codebase.

I'm still trying different things, but maybe someone here has run into this problem before, and can shed some light on it. None of my files are set to "read only", but even if they were, that wouldn't be a problem, seeing as I don't want to write to these files anyway.
markr
markr
Quote:
Original post by EGD Eric
But when I run this using internet exlporer 5.0, the applet won't load properly, and I get this error in the status bar:

exception: java.security.AccessControlException: access denied (java.io.filePermission Graphics\Ship_Blue.gif read)



Don't try to load files off the user's hard disc! Not only will the Applet not let you, they won't be there either.

Either use getResourceAsStream() (Which will load files from the jar) or something similar, or some file access method which loads stuff from the web server.

There are loads of examples.

Don't try to do local file IO, it won't work!

Mark
Glass_Knife
Glass_Knife
Yes, if you have stuff in a jar file, you must put all the images in the jar too.
// in the aplet code...
Toolkit toolkit = Toolkit.getDefaultToolkit();
ImageIcon icon = new ImageIcon( toolkit.getImage( getClass().getResource("/fileName.jpg")));

Note: I have had trouble loading a file from a jar file once because it needs a case-sensitive file name.

Hope that helps!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.