Open an applet in Linux Terminal - java

My requirement is to open an applet in Linux terminal.
I have created a java class and a html file named A.java and A.html as follows:
A.java:
public class A extends Applet
{
private int w, h;
public void init( )
{
System.out.println(" in init");
w = 45;
h = 50;
}
public void paint(Graphics g)
{
g.drawRect(w, h, 20, 80);
}
}
A.html:
<html>
<p> This file launches the 'A' applet: A.class! </p>
<applet code="A.class" height=200 width=320>
</applet>
</html>
When I try to execute it I get these errors:
[root#VMS e42]# appletviewer A.java
[root#VMS e42]# appletviewer A.html
Exception in thread "main" java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:204)
at java.awt.Window.<init>(Window.java:536)
at java.awt.Frame.<init>(Frame.java:420)
at java.awt.Frame.<init>(Frame.java:385)
at sun.applet.AppletViewer.<init>(AppletViewer.java:159)
at sun.applet.StdAppletViewerFactory.createAppletViewer (AppletViewer.java:98)
at sun.applet.AppletViewer.parse(AppletViewer.java:1158)
at sun.applet.AppletViewer.parse(AppletViewer.java:1092)
at sun.applet.Main.run(Main.java:156)
at sun.applet.Main.main(Main.java:98)
What do I need to change?

The error message speaks for itself: you need to provide an X11 server (and set your DISPLAY environment variable to point to it). Applets are AWT classes, and need a graphical display; you can't run one on an ordinary terminal.
As an aside, it's a bad idea to be running as root; it's an even worse idea to run X11 applications as root.

Related

java applet class not found exception in the chrome browser

I am trying to run Java applet using Google Chrome browser. Everytime I am getting no class found exception. Here is my code.
HelloWorld.java
package my.first.pack;
import java.applet.Applet;
import java.awt.*;
public class HelloWorld extends Applet {
/**
*
*/
private static final long serialVersionUID = 2741715258812838900L;
public void paint(Graphics g) {
g.drawString("welcome", 150, 150);
}
}
Hello.html
<applet code="my.first.pack.HelloWorld" width="300" height="300">
Sign your applet and all the .jar dependencies with a certificate.
Populate your manifest with all the tags mentioned below (it's in xml because I use maven, you can write in the way you prefer)
<codebase>http://location.of.your.jar/</codebase>
<permissions>all-permissions</permissions>
<Application-Library-Allowable-Codebase>http://location.of.your.jar/</Application-Library-Allowable-Codebase>
<Manifest-Version>1.0</Manifest-Version>
<Implementation-Title>App Name</Implementation-Title>
<Implementation-Version>0.1.0</Implementation-Version>
<Application-Name></Application-Name>
<Created-By>1.8.0_45</Created-By>
<Main-Class>package.YourClass</Main-Class>
<mode>development (or production)</mode>
<url>url of the application</url>
Surround your java method with the doPrivileged
Be sure that your browser has the java plugin enabled
Put your http path of your web app in the java exception list
If your url has _ (underscore/underline) probably it won't be recognized.
Try to move your .jar to the same folder of your html, not using the /applet folder.
Take a look on this post, I was having a similar issue.
Remember, this error saying that 'is not a function' is because your .jar is not loading - or you made something wrong with the js syntax, what I don't think so.

Applet doesn't run and asks for main method

I am a novice in java programming.This is my applet code for SimpleApplet.java:
import java.awt.*;
import java.applet.*;
/*
<applet code="SimpleApplet" width=200 height=60>
</applet>
*/
public class SimpleApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("A simple Applet",20,20);
}
}
I first compiled it in command prompt by javac SimpleApplet.java and then used java SimpleApplet. It throws up error that main class is not found in class SimpleApplet,please define main method.
Where am I wrong here ?
javac SimpleApplet.java
and then used
java SimpleApplet
Do instead:
javac SimpleApplet.java
appletviewer SimpleApplet.java
The trick here is that the applet viewer will read the source, and use the applet element defined in the comment to make an 'applet HTML' to run it.

Start: Applet is not initialized

I am beginner to Applets. Here is code for a basic applet to display string.
package firstjavaapplet;
import java.awt.Graphics; // program uses class Graphics
import javax.swing.JApplet; // program uses class JApplet
public class FirstJavaApplet extends JApplet
{
// draw text on applet’s background
#Override
public void paint( Graphics g )
{
// call superclass version of method paint
super.paint( g );
// draw a String at x-coordinate 25 and y-coordinate 25
g.drawString( "Welcome to Java Programming!", 25, 25 );
} // end method paint
public static void main(String[] args)
{
FirstJavaApplet obj = new FirstJavaApplet();
}
}
Following is HTML file I am using to include applet in webpage.
<body>
<applet code = "FirstJaveApplet.class" width = "300" height = "300">
</applet>
</body>
</html>
When I run Applet in appletviewer FirstJaveApplet.html , I get following:
String is not being displayed rather "Start: applet is not initialized."
<applet code = "FirstJaveApplet.class" width = "300" height = "300">
</applet>
The code attribute value should be the Fully Qualified Class name as opposed to the applet file name. So that should read:
<applet code = "firstjavaapplet.FirstJavaApplet" width = "300" height = "300">
</applet>
Note that the JRE will search for the class in a sub-directory of the HTML directory named firstjavaapplet. Unless the class is present in the right place, the problem will continue.
code = "FirstJaveApplet.class" : Java not Jave
I was also facing the same issue and none of the forum's solution rescued me :(
Then I realized, we need the set the size and visibility of the applet.
You can include the following constructor as your code:
FirstJavaApplet()
{
setSize(500, 500);
setVisible(true);
}
Applet don't need a main method to start
Just run without main method
I think you having the problem with your package name
compile without the package name package firstjavaapplet;
public class FirstJavaApplet extends JApplet
{
#Override
public void paint( Graphics g )
{
// call superclass version of method paint
super.paint( g );
// draw a String at x-coordinate 25 and y-coordinate 25
g.drawString( "Welcome to Java Programming!", 25, 25 );
} // end method paint
}
In the case of PApplet. On MacOS 10.10
You need to change the the JRE System Library min to 1.7.
In my case java.lang.UnsupportedClassVersionError: processing/core/PApplet : Unsupported major.minor version 51.0
Its throwing above. Its resolved when I'd set .JRE System Library-1.7
Hope its help you.
For PApplet
On Mac
Usually happens with JRE 1.6
Change the Build path as:
On your Project
JRE system Library > Build Path> Configure Build path
Choose JRE 1.7 or 1.8 (if installed it will be displayed)
Remove the old one from build path by right-clicking and choosing the right option
Try after placing the above java file,in a folder with name firstjavaapplet(pakage name) in the source pakagefolder.
Don't name your HTML file same as the Class File I think that will work.
Write the HTML code as below:
<html>
<body>
<applet code="firstjavaapplet.FirstJavaApplet" width ="300" height ="300">
</applet>
</body>
</html>
Save it as: FirstJavaApplet.html
Compile your Java Source File as:
javac -d . FirstJavaApplet.java
(You might have skipped compiling your Java Source File)
Then run your HTML file as:
appletviewer FirstJavaApplet.html
no need to create html file.whatever line you write in html also write in java after import statement
import java.awt.Graphics; // program uses class Graphics
import javax.swing.JApplet; // program uses class JApplet
/*<applet code = "FirstJaveApplet.class" width = "300" height = "300">
</applet>*/
public class FirstJavaApplet extends JApplet
{
// draw text on applet’s background
#Override
public void paint( Graphics g )
{
// call superclass version of method paint
super.paint( g );
// draw a String at x-coordinate 25 and y-coordinate 25
g.drawString( "Welcome to Java Programming!", 25, 25 );
} // end method paint
public static void main(String[] args)
{
FirstJavaApplet obj = new FirstJavaApplet();
}
}
No Need to write the main method in applet and you have to extend Applet than your applet will run
public class FirstJavaApplet extends Applet
I was also facing same problem because, i have changed path during installation of Older JDK (jdk 10.0.2)
to D Drive.
By doing this what happen is your main jdk-10.0.2 folder will install in C Drive's ProgramFiles folder and only copy of bin and some other folder is created in you newly mentioned path during installation.
So What You Can do is
Go to C Drive > Programfiles > java.
Cut Whole jdk-10.0.2(or older whichever yo have downloaded) and paste it wherever you want.
After pasting it go into jdk-10.0.2 folder -> Go to bin folder of it and write all your programs here.
i face the same,
but in my case i was using jdk17 to compile and using that .class file to run on appletviewer but appletviewer is closed after jdk9 so, any .class file that was created using jdk9 or higher it can't support so, i just used jdk8 to compile my program and it run fine on that appleviewer

JAVA applet Simplest program

I'm trying to run a code on lifecycle of applet as shown. This file is saved as Lifecycle.java
I compiled it by
javac Lifecycle.java
then tried to run it by
appletviewer Lifecycle.java
package APPLETS;
import java.applet.Applet;
public class Lifecycle extends Applet
{
/*
< APPLET
code = "Lifecycle.class"
height = "300"
width = "300">
< \APPLET>
*/
public void init()
{System.out.print("INIT");}
public void stop()
{System.out.print("STOP");}
public void start()
{System.out.print("Start");}
public void destroy()
{System.out.print("Destroy");}
}
APPLET is not loading then, though my code compiles successfully, no instructions are seen on command prompt. I'm just seeing a blank page with error -> Start:applet not initialized
HERE is the Lifecycle.html code-->
and here is the ERROR-
load: class APPLETS.Lifecycle.class not found.
java.lang.ClassNotFoundException: APPLETS.Lifecycle.class
The appletviewer is expecting to find HTML content so cannot parse the input file. Use appletviewer against a URL rather than a Java source file.
appletviewer is used to view applets using a URL. This URL can be in the format of a local or remote HTML document. Create a HTML document including the tag specifying your class and run the appletviewer against it.
life.html:
<APPLET CODE="APPLETS.Lifecycle" width="300" height="300"></APPLET>
then use
appletviewer life.html
The simplest folder structure for this to run is
./
|life.html
|-APPLETS
Lifecycle.class
Related: The Java Applet Viewer
Aside: Consider using the more up-to-date Swing JApplet.
Put Lifecycle.java in a folder called APPLETS, and try running:
appletviewer APPLETS.Lifecycle

Java Code Compile Error

I am trying to compile a 4KB Java game called "Left 4K Dead".. anyways, It will compile successfully with the javac G.java command, but when you go to run it using java G it spits back this error at me:
Exception in thread "main" java.lang.NoSuchMethodError: main
Anyone know how to make this work? Thanks :)
Beginning of code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
public class G extends Applet implements Runnable
{
private boolean[] k = new boolean[32767];
private int m;
public void start()
{
enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
new Thread(this).start();
}
public void run()
{
BufferedImage image = new BufferedImage(240, 240, BufferedImage.TYPE_INT_RGB);
Graphics ogr = image.getGraphics();
Exception in thread "main" java.lang.NoSuchMethodError: main
This exception indicates exactly what it states, there is no main method, so the program cannot start.
The issue with the Left 4k Dead code is it is an applet. It expects to be compiled and then run from a webpage, not run from command line (i.e. you can't run it with the command java). If you wish to run it from the command line, you should look into a standalone applet viewer.
After compilation of the class, use
appletviewer <class-name>
to run it through the commandline since it is an applet.
OR ELSE
you can embed it in a browser as CodeMaker suggests.

Categories