Trouble running an applet made in Eclipse in a browser - java

I have made a Java applet game in Eclipse which has many classes and media associated with it. I have now been trying to finally test the game in a browser but I am having a hell of a hard time getting it to work.
I have exported a .jar file (a non-runnable, could that be a problem?) and tried many different ways of loading the applet. I have read over the materials on the oracle website as well.
My first two basic questions are:
My applet does not have a static void main(String args[]){ line because I was under the impression that for applets you use a init() and start() method. Could this be the problem?
if not, my class which contains the init and start and the other basic methods is called Start.class and is located in a bin/ directory. Am I able to edit the manifest which is included in the .jar exported from eclipse and but this Start.class as the main class using Main-Class: Game.Start?
Another very basic question also when it comes to directories in java or specifically in .jar archives, are folders in a path names always separated by a .? or do /'s work too?
My project name is simply Game, I have a src folder with .java files and a bin folder with .class how do I direct the manifest to the bin/start.class file?
Sorry this has been rather frustrating especially because I really want to be able to share this applet. Any help would be greatly appreciated.

1) My applet does not have a static void main (args[]){ line because I was under the impression that for applets you use a init() and start() method. Could this be the problem?
No, this is not a problem, as Applets and JApplets do not use main methods to run. Note that some may have main methods that may be used to allow the coder to test the code in a non-applet environment, but when run as an applet, the main methods are ignored.
2) Another very basic question also when it comes to directories in Java or specifically in .jar archives, are folders in a path names always separated by a "."? or do "/"'s work too?
the directeries in jar files use "/". Please check that you are not trying to use resources as Files since jar files do not hold files (but rather resources).
My project name is simply Game, I have a src folder with .java files and a bin folder with .class how do I direct the manifest to the bin/start.class file?
Consider showing the structure of your jar file and also a small test html file where you try to run the applet.

Start.class and is located in a bin/ directory
I agree with Hovercratft about a missing main (not missing), about runnable jar (not necessary).
One problem which might exists is the exact location of files and naming of classes.
If your class name is bin.Start, because you defined a package bin (very uncommon), you need to put the bin directory into the jar.
If you didn't declare a package, you don't have a directory to put into the jar. Maybe eclipse handles this for you automatically.
To test your applet, you need a html file, and start the html file in the appletviewer. If this works, you test it in the browser. Else verify that you can start foreign applets in your browser, to make sure it is installed in the correct way.
Since you show a sloppy habit in the question of cases: Start.class or start.class: This is significant for Java. Use initial uppercase names everywhere.

Related

Absolute file paths when running program from jar?

I don't understand how Java jar files work. I am trying to understand what is possible and not possible when creating a Java jar file. Is it possible to have a String path running normally in a Java jar file? Will this normally work as it works when running main class in eclipse? I mean, I have an absolute path in my main class that grabs the file and reads from it.
public static final String file1 = "C:\\Users\\Documents\\test1.txt";
public static final String file2 = "C:\\Users\\Documents\\test2.txt";
This is what I have when running my program and it works fine. This is inside a class that is called somewhere along when I want to read a file. My question is... will this prevent my jar file from working properly normally AS when running the main class from eclipse?
I have the jar file but what if it doesn't or does it still look for file1 and file2?
It doesn't matter whether that code is in a jar file or not. The strings will still be exactly as they are, and if you pass them to methods that look for files with those paths, it'll look for files with those paths in the file system of the machine where the code is running. It won't look for them inside the jar file.
A Jar file is basically an executable of your project, it is used for example by frontend's who need a backend but don't want to open an IDE for compiling and executing purposes. Your Jar file contains .class files responsible for the execution of your project, you an execute your jar in a server too, so your application will run for more people (if you configure right).

Passing from java to HTML

I have i problem, I made a program in java using eclipse compilator, the thing is that It has two packages and in each package there are 3 classes i have tried to insert this code in a html file useing the applet tag but the problem is that it doesnt works, i dont know how to do it, and its quite important
I have tried using applet tags and tried the .jar I found something like this
<applet codebase ="." code="zuve.ZuveApplet.class"
archive=".jar,1.jar"
height="1000" width="1000"/>
I recomend you do a short tutorial on Applets:
http://java.about.com/od/webapplications/ss/firstapplet_3.htm
It is quite easy and you may made a misstake at one of those important points:
Compile your source: This includes a.) having your .java files beeing transformed to .class files and b.) packaging everything into a .jar file (you can make a .zip and rename it to .jar by hand).
Link your applet: This is done like described here: http://docs.oracle.com/javase/tutorial/uiswing/components/applet.html. You want to have both of your different packages packed in ONE .jar and have that single jar defined as argument 'archive' while your argument Code has to point to your applet class (e.g. the one extending from either Applet or JApplet or something like that).
Make sure that paths are set properly, easiest way would be to have everything (the .html with the applet tag and the .jar with your applet code inside) inside the same folder.

getResourceAsStream works in Eclipse IDE but not in JAR format

This is a really common error, because there are tons of threads about it, but I'm not sure if since my situation is slightly different from all of them the solutions don't work?
Basically, I'm in eclipse. I have a source folder called src, then I have a package that goes down three folders, then the class in question. The class uses the code:
BufferedImage im = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream(filenames[x].concat(extension)));
surrounded by a try/catch. filenames is an array of all the file names I am loading (this code is run multiple times in a for loop) and extension is ".PNG". The pictures are located in another source folder called EngineTextures.
Running this program works fine in Eclipse! All textures are loaded and all my other code runs! However, I export it as a runnable jar and run it in command prompt to recieve input == null errors on all of them, pointing to the line that has ImageIO.read(Thread.currentThread() in it.
The kicker is this whole thing worked in a separate project before, and when I even tried re-exporting that project, I recieve the same errors on completely unchanged code. This leads me to believe I have some obscure Eclipse setting changed wrongly.
Opening the jar, my MANIFEST.MF has a version of 1.0 and a classpath of just plain ., which I thought was correct for this kind of thing? The Main-Class points to the right place, and all my pictures are right there next to the META-INF folder.
Solutions I've looked at unsuccessfully:
getResourceAsStream working in eclipse, but not when run as applet in browser
Why does getResourceAsStream() work in the IDE but not the JAR?
Java IDE - Eclipse, Importing resources
Audio file in jar made by Eclipse IDE
getResourceAsStream() returning null in jar but fine in eclipse
Additionally, I completely deleted the workspace and recopied my pictures and code into the same state, thinking maybe some .metadata thing was wrong, to no avail.
Thank you, in advance, for any and all help. I hate to make a repeat like this but no solutions have worked thus far. Please let me know if I have not given any crucial information.
Opening the jar, my MANIFEST.MF has a version of 1.0 and a classpath of just plain ., which I thought was correct for this kind of thing?
No. The Class-Path entry in a JAR file names other JAR files, relative to the location of this jar file. It doesn't name directories:
"The value of this attribute specifies the relative URLs of the extensions or libraries that this application or extension needs."
That in turn implies that resources to be loaded via getResourceAsStream() must be in JAR files.

Java Game refering to files

I have started getting into game programming.
My question is, that when I am working with files, either parsing data, writing to files, etc. Should I be using relative path names, or absolute pathnames, or something else which is better. I've heard about using jar files, but I am not sure
1. how that works
2. if it is a good way to do it.
So when developing a game that will be cross platform, what is the best method for managing files that the program will need to read from and write to.
there are several ways in which you can ship your code as a product. the most common are
packaging everything in one executable jar file.
having a set of folders where you place all necessary resources.
minecraft, for example, is written in java and distributed as a single executable jar file that contains all necessary class files and resources. to run the game (assuming you have java installed) all you need to do is double-click the jar file.
read this short tutorial about how to add a main class to a jar file.
either way, always treat classes and resources in your code as if they're in your classpath. for example, if you have a my.properties file on the root of the source tree then load it by using 'my.properties'. if you put it under a 'conf' folder then use 'conf/my.properties'.
i think it is the safest way not to get lost.
are you using maven?
The jar file is a zip of all your compiled *.class files and your resources. You can safely load your resources and even default data FROM a jar if you package your program, but you can NOT safely write data back to the jar. This detail is answered in depth already at
How can an app use files inside the JAR for read and write?
For information on how to package a jar see
http://docs.oracle.com/javase/tutorial/deployment/jar/

Java, running the code

I have a .java file that I created in eclipse. I managed to make a .jar out of it, but I don't know how to put my images folder into the jar, nor do I know how to make the jar run?
Should it run already, or would it just be an archive like a .zip?
Thank you guys so much for your help here, because I am completely and totally lost when it comes to this. I have been programming in java (just making .java files in JCreator basically) for about 2 years now.
I have searched and searched but never found a good answer. Anyone that can shed the like on how running .java files as an application and things work, that would be awesome.
P.S. I am using GUI, and the code is correct.
Thank you so much!
What is a runnable jar?
A jar is a archive (it uses the same file format of a ZIP) that contains program artifacts: compiled classes, images, configuration files and other resources.
To be a runnable jar it must contain a special file, called manifest (the manifest support electronic signing, version control, package sealing) with a Main-Class: project.name.EntryPoint entry that specifies the class that is the start point of the program.
This can be easily achieved by using the Eclipse feature File > Export > Java > Runnable JAR file.
How to run a runnable jar?
Using the command line, just do java -jar <jar_file_path>.
But nowadays the graphic shells of the operating systems (Windows Explorer / Gnome / KDE) already call java when you double-click a jar file.
How to put images on the .jar?
You can do this specifying on the Eclipse project that the images folder is also a source folder. Now when exporting the project to the jar file it will include them.

Categories