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
Related
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.
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.
Hi everyone I made a calculator applet and am trying to export it as a JAR file, since you can't export applets as runnable JARs. When I try to do java -jar Calculator.jar in command line though I get an error "saying no main manifest attribute, in Calculator.jar". I'm wondering if I need to do anything else in my main method other than below to fix this?
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
To use an applet, you have to embed it in a webpage.
Here's some HTML for your use case.
<applet code = 'PACKAGE.Calculator'
archive = 'Calculator.jar'
width = 300
height = 300>
<param name="permissions" value="sandbox" />
</applet>
Fill in PACKAGE with your package for the applet and modify the height/width to fit.
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
I am running my project on NetBeans but I am not able to run .jar file successfully.
1 . I was developing my project using Java Class library , and then later found there is some problem in manifest files . .
2. Then to solve above problem I created project as JavaApplication project, now manifest looks like this
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_22-b22 (Sun Microsystems Inc.)
Class-Path:
Main-Class: gameloftbraker.GameLoftBraker
on terminal it throws following exception:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:204)
at gameloftbraker.Ball.<init>(Ball.java:26)
at gameloftbraker.Arena.gameInit(Arena.java:71)
at gameloftbraker.Arena.addNotify(Arena.java:67)
at java.awt.Container.addNotify(Container.java:2584)
at javax.swing.JComponent.addNotify(JComponent.java:4687)
at java.awt.Container.addNotify(Container.java:2584)
at javax.swing.JComponent.addNotify(JComponent.java:4687)
at java.awt.Container.addNotify(Container.java:2584)
at javax.swing.JComponent.addNotify(JComponent.java:4687)
at javax.swing.JRootPane.addNotify(JRootPane.java:754)
at java.awt.Container.addNotify(Container.java:2584)
at java.awt.Window.addNotify(Window.java:707)
Thanks for help in advance. I hope I am not missing anything. There is no problem in code as it build and runs successfully , If code is needed let me know .
Ball.java
////////////////
package gameloftbraker;
import javax.swing.ImageIcon;
public class Ball extends Graphc implements Commn {
protected String ball = "../images/ball.png"; //address to ball graphic
protected int xdir;
protected int ydir;
public Ball(){
xdir = 2;
ydir = -2;
ImageIcon img = new ImageIcon(this.getClass().getResource(ball));
image = img.getImage();
height = image.getHeight(null);
width = image.getWidth(null);
resetState();
}
// rest of the functions * it works perfectly as project but unable to run in .jar
}
It would help if you'd show us the code for Ball, highlighting the lines shown in the stacktrace... but my guess is that you haven't bundled the graphics for your game into the jar file correctly.
One thing to point out:
There is no problem in code as it build and runs successfully
Well it's not running successfully now, is it? Building and running successfully in one environment doesn't mean there's no problem in the code. For example, you could have hard-coded filenames in there which aren't present on any other machine - I'm not suggesting that actually is the case here, I'm just saying that "works on my machine" isn't proof that there's no problem in the code.
EDIT: Now that we can see your code, it looks like ball.png may not be in the jar file. Or the problem is your use of .. in a resource name. Try changing the resource name to:
// TODO: Make this private static final unless you have any reason not to...
protected String ball = "/images/ball.png";
If you want to use data (e.g. picture) outside the jar just use this:
ImageIcon img = new ImageIcon(ball);
on IDE run you have only folder, so it works. In jar file you have to leave the jar file and I think it's not possible with your way