Applet doesn't run and asks for main method - java

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.

Related

How to correctly create and import packages in Java

I am using Netbeans 8.1 and Java 8.
I have a Java program named "MyFrame.java" and I want to create a package with its classes and methods - I call this package "myframe" and it is located at "\Lab\MyFrame\src\myframe". See picture:
(Ignore the red lines - this is a dummy version).
The class file is created after compiling, using the command "javac MyFrame.java", in the same directory \myframe. Now I want to import the package "myframe" in a new Java file "MoreButtons.java". So it would look like this and for convenience I save it in \src:
Compiling and executing MoreButtons.java works fine. The package has been imported. But now MyFrame.java is a bit trickier to execute: the naïve approach yields:
Translation: Error: Could not find or load main class
This seems to be quite a common problem and one of the solutions is simply to add the directory (\myframe) to the PATH environment variable. However, doing this still produced the error.
1) What am I doing wrong and how can I fix this?
2) What is the correct way to create and import custom-made packages in Java?
Make sure that terminal is at path Lab\MyFrame\src:
javac myframe\MyFrame.java MoreButtons.java
java -cp .; myframe.MyFrame
P.S. (/,:=linux/mac) or (\,;=windows)
MyFrame.java
package myframe;
public class MyFrame extends javax.swing.JFrame{
public MyFrame(String title){
super(title);
setSize(200,100);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
}
MoreButtons.java
public class MoreButtons {
public static void main(String[]args){
new myframe.MyFrame("More Buttons");
}
}

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.

Why isn't JOptionPane showing up?

I made a simple JOptionPane class that should pop up with a string, when I type:
javac Hellodialog.java
nothing happens at all. No error messages but nothing comes up.
import javax.swing.JOptionPane;
public class Hellodialog
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "fsdfsdfdsfds");
}
}
compile into a java .class file
javac Hellodialog.java
run the java class
java Hellodialog
You use the javac command to compile your source to byte code, and the java command to run your compiled code.

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