Starting a class in Java, but different - java

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.

Related

Run Java application with minimal code (hiding the main-method) [duplicate]

There has been a history of "how to launch an application without a main method" questions, most stating something along the lines of "you can't". I noticed how JavaFX applications can now be launched simply by extending Application. I've been using a method similar to Application.launch(), but I've always seen the main method as boilerplate code in that situation. So I started looking for answers again.
I found this answer linking us to where we can learn about creating our own JVM launchers, but sadly the article no longer exists. I've spent quite a while looking for anything that might hint me on what I should do, but no results.
My Question
How would one create a custom JVM launcher to launch an application based on what is being extended? For example, imagine a project with 1 declared class:
class MyApplcation extends App {
}
No main method is needed for the project to be launched, since it extends App. This is not the question, although it is my target goal. The question is how could someone create a custom JVM launcher?
My guess is that it would involve using some kind of base, to give us a skeleton of some sort, but I'm not sure where to find such a thing.
How JavaFX Implements its Application Execution Model
JavaFX applications which contain no main method can be launched because of the implementation of JEP 153: Enhance the java command-line launcher to launch JavaFX applications.
This was modification of the openjdk launcher logic to check if the class to be launched extends Application, and, if so, call out to a JavaFX specific launcher which started up the Java runtime and associated threads, created a GUI window (in JavaFX terms a Stage), then, created an instance of the avaFX application and invoked init and start methods on the application as per the the JavaFX application lifecycle rules.
Tracing the implementation through in code:
main.c for JVM invokes JLILaunch in java.c
java.c looks for a Java class sun.launcher.LauncherHelper and calls into the java class using JNI, invoking the checkAndLoadMain function.
If the class to be launched has no main method, but does extend the JavaFX application class, a FXHelper class is created holding the launch parameters.
A reference to the FXHelper is returned to java.c via JNI.
java.c invokes the main method on the FXHelper via JNI.
The FXHelper uses reflection to invoke com.sun.javafx.application.LauncherImpl.launchApplication().
The JavaFX LauncherImpl will setup the application classloader, then first try to invoke a main() method on the main class of the application.
If there is no main() method, the JavaFX LauncherImpl will start up the JavaFX launcher thread.
On the launcher thread, the LauncherImpl will call init() on the JavaFX application.
On the JavaFX application thread, the LauncherImpl will create a new Stage (window) and pass it to the start method of the application.
When the last stage is closed or the JavaFX platform is exited, the stop method on the application is called by the LauncherImpl.
All the call traces return and the application exits because there is nothing more to do.
How you could customize the launcher
Grab a copy of the sun.launcher.LauncherHelper code.
Ignore the warning at the top of the code "This is NOT part of any API supported by Sun Microsystems. If you write code that depends on this, you do so at your own risk. This code and its internal interfaces are subject to change or deletion without notice." and start modifying the code anyway.
Create your own version of the FXHelper which will invoke your own LauncherImpl rather than the JavaFX one (in the case that the target main application class extends your application framework class rather than the JavaFX one).
Write your own LauncherImpl that sets up the classloader for your application, creates an instance of your application class and invokes whatever launch entry points you want on your application type.
Run your applications, placing the hacked version of the sun.launcher.LauncherHelper and your LauncherImpl on the boot class path, so they get picked up instead of the default versions which ship the JDK.
Sample launch command, assuming the target application to be launched is com.mycompany.MyApplication:
java -Xbootclasspath/p <launcher class directory> com.mycompany.MyApplication

How would one create a custom JVM launcher?

There has been a history of "how to launch an application without a main method" questions, most stating something along the lines of "you can't". I noticed how JavaFX applications can now be launched simply by extending Application. I've been using a method similar to Application.launch(), but I've always seen the main method as boilerplate code in that situation. So I started looking for answers again.
I found this answer linking us to where we can learn about creating our own JVM launchers, but sadly the article no longer exists. I've spent quite a while looking for anything that might hint me on what I should do, but no results.
My Question
How would one create a custom JVM launcher to launch an application based on what is being extended? For example, imagine a project with 1 declared class:
class MyApplcation extends App {
}
No main method is needed for the project to be launched, since it extends App. This is not the question, although it is my target goal. The question is how could someone create a custom JVM launcher?
My guess is that it would involve using some kind of base, to give us a skeleton of some sort, but I'm not sure where to find such a thing.
How JavaFX Implements its Application Execution Model
JavaFX applications which contain no main method can be launched because of the implementation of JEP 153: Enhance the java command-line launcher to launch JavaFX applications.
This was modification of the openjdk launcher logic to check if the class to be launched extends Application, and, if so, call out to a JavaFX specific launcher which started up the Java runtime and associated threads, created a GUI window (in JavaFX terms a Stage), then, created an instance of the avaFX application and invoked init and start methods on the application as per the the JavaFX application lifecycle rules.
Tracing the implementation through in code:
main.c for JVM invokes JLILaunch in java.c
java.c looks for a Java class sun.launcher.LauncherHelper and calls into the java class using JNI, invoking the checkAndLoadMain function.
If the class to be launched has no main method, but does extend the JavaFX application class, a FXHelper class is created holding the launch parameters.
A reference to the FXHelper is returned to java.c via JNI.
java.c invokes the main method on the FXHelper via JNI.
The FXHelper uses reflection to invoke com.sun.javafx.application.LauncherImpl.launchApplication().
The JavaFX LauncherImpl will setup the application classloader, then first try to invoke a main() method on the main class of the application.
If there is no main() method, the JavaFX LauncherImpl will start up the JavaFX launcher thread.
On the launcher thread, the LauncherImpl will call init() on the JavaFX application.
On the JavaFX application thread, the LauncherImpl will create a new Stage (window) and pass it to the start method of the application.
When the last stage is closed or the JavaFX platform is exited, the stop method on the application is called by the LauncherImpl.
All the call traces return and the application exits because there is nothing more to do.
How you could customize the launcher
Grab a copy of the sun.launcher.LauncherHelper code.
Ignore the warning at the top of the code "This is NOT part of any API supported by Sun Microsystems. If you write code that depends on this, you do so at your own risk. This code and its internal interfaces are subject to change or deletion without notice." and start modifying the code anyway.
Create your own version of the FXHelper which will invoke your own LauncherImpl rather than the JavaFX one (in the case that the target main application class extends your application framework class rather than the JavaFX one).
Write your own LauncherImpl that sets up the classloader for your application, creates an instance of your application class and invokes whatever launch entry points you want on your application type.
Run your applications, placing the hacked version of the sun.launcher.LauncherHelper and your LauncherImpl on the boot class path, so they get picked up instead of the default versions which ship the JDK.
Sample launch command, assuming the target application to be launched is com.mycompany.MyApplication:
java -Xbootclasspath/p <launcher class directory> com.mycompany.MyApplication

Java Applet runs without a main method?

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()?

Run a non-applet .jar game on a browser

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.

Calling a class in the 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

Categories