I am a beginner in java game programming. I have developed a simple java game and obtained a .jar file of it. It is not an applet. I would like to run it on a browser. Is that possible? How can I achieve that?
Assuming your jar's main class simply opens a JFrame to show its contents, you can build a wrapper applet class which simply invokes it, like this:
public class WrapperApplet extends Applet {
public void start() {
new Thread("application main Thread") {
public void run() { runApplication(); }
}.start();
}
private void runApplication() {
my.Application.main(new String[0]);
}
}
If you want it nicer, have the applet show a button and start the main method only after the button is clicked.
If you want to embed a java application in a web page, you need it to be in applet form. It's not that difficult to convert them, see this link for a bit of help.
It's possible with Java Web Start. From the Wikipedia article:
Web Start can also launch unmodified applets that are packaged inside .jar files, by writing the appropriate JNLP file. This file can also pass the applet parameters. Such applets also run in a separate frame. Applet launcher may not support some specific cases like loading class as resource." The same article mentions some of the problems with applets "Web Start has an advantage over applets in that it overcomes many compatibility problems with browsers' Java plugins and different JVM versions.
This SO question explains some of the tradeoffs on Applets v. JWS... In my opinion, if you expect a lot of people on different types of systems to use your application, or if it uses a fair amount of memory (likely with a game), JWS is better.
Related
In my Computer science class we started classes in Java Eclipse like so:
public class FirstApp extends Applet implements ActionListener, MouseListener, MouseMotionListener
{
Not the way I see it everywhere else:
class FirstApp {
public static void main(String[] args) {
...
}
}
What's the difference? What would happen if I changed a piece of my code to the second one?
The difference is that your first example uses Applet technology, which is now widely deprecated. The browser will run it for you inside a JVM which it creates.
A Java applet is a special kind of Java program that a browser enabled
with Java technology can download from the internet and run. An applet
is typically embedded inside a web page and runs in the context of a
browser. An applet must be a subclass of the java.applet.Applet class.
The Applet class provides the standard interface between the applet
and the browser environment.
Your second example is a Java application designed to be invoked directly by the JVM. Command line arguments will be passed as a String array into main(). i.e.
java -cp . org.example.FirstApp
As Adam pointed out in his answer, the difference between both examples is that the first program is a Java Applet and the second a normal Java Application.
The applet
The applet is run in your browser which will start a Java interpreter for you and create the applet. After the creation of an applet, the method init() is called by the Java interpreter, when initialized, the applet will be started via the method start(). In your first example, no code is run.
To add some code to execute, you have to implement the start() method, which will make your applet look like this:
public class FirstApp extends Applet implements ActionListener,
MouseListener, MouseMotionListener
{
public void start() {
// Code to execute when started
}
}
The interfaces implemented are event listeners that enable the applet to react to certain user events. A normal application does not necessarily implement these as command line application don't need graphical event listeners.
You can either launch your applet from a browser or from Eclipse, like you probably did in your Computer Sciene classes. When an applet is launched via Eclipse, Eclipse takes care of launching and displaying the applet in an own window so you don't have to write the web page in which the applet will be embedded.
The application
The application is run in a Java interpreter started directly by you, the user. When started, the Java interpreter runs the main(String[]) method, so the equivalent to the upper example would be:
public class FirstApp {
public static void main(String[] args) {
// Code to execute when started
}
}
After you've compiled it (via javac FirstApp.java), a normal application can be started via command line, like this:
java FirstApp
Of course, applications can also be launched (i. e. compiled and interpreted) through Eclipse with the "Launch" button.
I am new to Java Swing. I just started to learn about Swing.
I see these tutorial videos to develop GUI for ones needs using Net-beans/Eclipse. My question might be simple/funny to most of you guys.
Lets say GUI is developed to do basic calculation like addition/multiplication and when the project is run on Eclipse/Net-beans its doing perfectly fine.
How do we bundle the code and give it to users so that they can the GUI to do any calculation?
Is it something that will be bundled as setup or exe file?
Basically, how do other use this UI?
What you're looking for is called Building which creates a jar file (.jar) and contains intermediate code (bytecode) which allows users to run the code like an executable (.exe) file.
Remember: You'll need a place for the program to start from (you probably already have this if you can run the program from your IDE) (ie: public static void main (String[] args) { ... })
Refer to this link for building in netbeans, and this one for building in eclipse.
I currently have a in-development Java Game.
It runs from a .jar, with all the image files inside. The .jar creates and accesses files in the working directory.
I was wondering if there was a simple way to put this on a webpage as a Java Applet. I currently have Applet code in the Game, but all it does is calls the normal main method to create JFrames and run the game.
I need a simple way to run this on clients from a webpage, prefferably an applet? Is there one?
Please note, I didn't actually make this as an Applet at first. It's currently a .jar, with a .bat to run it. My "Applet" class is this simple...
package explorer.applet;
import java.applet.Applet;
import explorer.boot.Startup;
#SuppressWarnings("serial")
public class ExplorerApplet extends Applet{
public void init()
{
Startup.wp = true;
Startup.main(null);
}
}
I was wondering if there was a simple way to put this on a webpage..
Sure. Launch a JFrame direct from a link using Java Web Start.
..as a Java Applet.
Why? Applets are harder to deploy and maintain, and provide a less satisfactory experience to the end user.
Note that the fundamental problem is the same either way. 'How to access an application resource?'
Such resource access would be by URL. There are these 2 primary alternatives:
Add the resource to the Jar and use Class.getResource("/path/to/the.resource")
Put the resource 'loose' on the home server, and form the URL relative to the code base or document base of the applet, or the code base of the JNLP (the file used to configure a JWS launch).
The .jar creates and accesses files in the working directory.
About 4MB, and they store the game information. (It's a 2D world game.)
They also have to be client side, and in the folder that the "jar" runs from.
That is too large for any of the sand-boxed techniques I had in mind, but since it is 'static'1 resources - they can be added to a Jar which is on the run-time class-path and thereby will be 'downloaded and made available to the app.'.
Access the resources as described above.
By 'static' I simply mean 'do not need to be edited or changed by the user or app.', as opposed to something like 'high scores' which must logically be written by the app. on game exit. It is still possible to update the maps, all you need to do is provide the updated Jar and Java will do the rest.
I was running a Java class that extends Applet implements Runnable and apparently the program can run, but there is no main method. I thought Java applications needs the main method as its entry point?
Java Applets have an init method instead of main.
It's:
public void init() {... }
Yes, but applets aren't applications. There is a main method in the applet runner (assuming it's implemented in Java; it need not be) but the applet doesn't work that way; it gets loaded/instantiated from a file and then it proceeds along its lifecycle through initialization, starting, operating, stopping, and finally being destroyed. The code that sends it through these states is hidden from the applet's view; it just knows its in an environment that can run applets.
Applets differ from stand-alone Java applications in that they do not need to implement a main method.
Life Cycle of an Applet
Copied from google results:
Applets are standalone programs which require a third party tool for its execution that is either it is java enabled web browser or applet runner. So it doesn't have main(). It is
possible to run a program without main.
Possible duplicate of:
Why do applets not need a main()?
I have written a class that is an applet and doesn't contain a main().
Is there any possible way for me to just pass the entire class to main to run it because I can't call all the methods through main, I just use so many things it's impossible.
public static void main(String[] args){ }
public class Travel extends Applet implements MouseListener{
}
It seems Applets don't run main().
This hybrid demo can not only be run as either an applet or application, but be launched direct from the command line using the source in the applet viewer.
E.G.
prompt> javac HybridApplet.java
prompt> java HybridApplication // Note the 'Application'
prompt> appletviewer HybridApplet.java // Note the '.java'
Exiting the applet in the browser should redirect to the source. It will have no effect in the applet viewer. Applet viewer does not support showDocument(), unlike Appleteer, which does ;).
Edit: Note though, that many things designed as applets leverage methods & classes useful to applets - getClip(), getDocumentBase()..
These are for convenience and mostly have equivalents in other non-applet classes.
Create a main method in your applet class and instantiate it from within the main method.
If you really want to run the applet though, I suggest using the appletviewer