Exporting an applet as jar - java

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.

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.

Deploying a JApplet: ClassNotFoundException

Edit 2: I created a html in the root directory and placed the jar file in the same place, I could then get the applet to run (though I had some issues with self-signed security). This tells me the problem is in the applet code. Any ideas why it wouldn't find the class when if I can remove the codebase attribute it runs correctly?
Edit 1: I have updated the entry point to remove the frame. I also tested with the HelloWorld applet and still received the same error.
I'm fairly new to Java so I'll try to explain my problem as clearly as I can and with plenty of detail. If I miss anything please let me know. I'm also aware that this problem is frequently asked about on here, I've done a fair bit of research and found conflicting responses and nothing that worked.
I have developed a JApplet in eclipse, exported a jar file for the project and am attempting to deploy it on my website. However, when I attempt to view the applet online I get the error: ClassNotFoundException. It is probably also worth mentioning that I am trying to deploy this JApplet through wordpress.
Here is the html code I'm using to deploy:
<applet code = 'gui.ConverterGUI.class'
codebase = 'http://www.myurl.co.uk/Java/'
archive = 'AConverter.jar'
width = 800
height = 600>
<param name="permissions" value="all-permissions" />
</applet>
My applet has a few packages and classes which I think I have set up and exported correctly, but incase that is causing problems, here is my main entry point:
public class ConverterGUI extends JApplet {
// Current program ver.
public static final double VERSION = 0.0;
public void init() {
// Make it look nicer.
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private void createAndShowGUI() {
System.out.println("Created GUI on EDT? "+
SwingUtilities.isEventDispatchThread());
//JFrame f = new JFrame("Converter GUI");
ResultDisplay resultDisplay = new ResultDisplay();
getContentPane().add(resultDisplay, BorderLayout.CENTER);
getContentPane().add(new InputFields(resultDisplay), BorderLayout.NORTH);
}
}
This is my project layout.
I've exported the jar using Eclipse, I've got the impression that this means it has been signed properly already, but if this is not the case and it is causing issues, I'd appreciate being pointed in the right direction to do this (I do have eclipse set up with the JDK rather than JRE).
The issue was in the html code and the way wordpress was interpreting it. For future reference, I fixed the issue by removing the lines from the html code and using double quotes, as follows:
<applet code = "gui.ConverterGUI.class" codebase="http://www.myurl.co.uk/Java/" archive="AConverter.jar" width=800 height=600><param name="permissions" value="all-permissions" /></applet>
Nonetheless I did get some useful advice here. Thanks all.

How to open your eclipse project in a window?

How do I export my Eclipse project so that it is its own console application sort of thing.
Every time I try to run the .jar file after exporting it, a window pops up saying that it couldn't open the program. Is there some code I need to enter in order to make it its own window, like when you run it in eclipse, except it is its own window and application. Here is an example of how I have coded it:
public class main {
public static void main(String[] args) {
Scanner text = new Scanner(System. in );
System.out.println("Ready:");
boolean loop2 = true;
while (loop2 = true) {
String text1 = text.nextLine();
switch (text1) {
case "hi":
System.out.println("Greetings!");
break;
}
}
}
}
Any help would be nice! I am trying to make this for sending to my friends, too, just so you know.
In the navigator view, right-click on your project > Export > Java > Jar. Be sure to indicate your class as main class.
Then, once saved, you can run the jar by double clicking on it, or using java -jar jarfile.jar from command line.

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

Categories