Deploying a JApplet: ClassNotFoundException - java

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.

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.

Java Swing window not appearing in Eclipse

I have this code below to display a window using Java Swing. The problem is when I run the code in eclipse the window does not show. When I export the file as an executable JAR file and run it it works. Is there a bug with eclipse that prevents it from running it from there?
What am I missing?
package com.gui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
public class Calculator {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculator window = new Calculator();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Calculator() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
//frame.setBounds(100, 100, 450, 300);
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblNewLabel = new JLabel("Hello World");
frame.getContentPane().add(lblNewLabel, BorderLayout.NORTH);
}
}
Some projects created by Eclipse/WindowBuilder in macOS have this window-not-showing-up problem.
When an app is created through New > Project > WindowBuilder > SWT Designer > SWT/JFace Java Project, the new project automatically includes several extra jar files in classpath. Certain jar file(s) trigger eclipse to use special arguments (-XstartOnFirstThread) in the command (ps aux | grep to find out) when starting the application, and startOnFirstThread is giving us the problem here. AFAIK, -XstartOnFirstThread is added only in MacOS.
Solution 1: don’t use “WindowBuilder > SWT Designer > SWT/JFace Java Project” to create the project. You can just create a regular java project, and add the stuff you need.
Solution 2: remove the jar files. Right click on project > Properties > Build Path > Configure Build Path > Libraries, remove org.eclipse.swt (removing this one suffices in my case, there might be others in your case).
I had the same problem, and this is what worked for me (after the help of some folks on stackoverflow).
It turns out I had a library problem. I had had imported all the jars in the .lib directory from jfreechart. In reality only two were needed and some unnecessary ones were labeled swt and experimental. Once I removed all the ones that were not needed, did a clean, and rebuilt, everything worked fine.
Oddly, changing the order of the jfreechart library (which included the conflicting jars) to the bottom did not help, the extra jars had to be removed.
Not a jfreechart issue, obviously my own library import issue. So I suggest you try to remove some of the libraries that may be conflicting, then clean, build, and run again. Good luck.
Had the same issue and the root cause turned out to be the extra .jar in the build path(using macOS). For me, it worked after removing the org.eclipse.swt.cocoa.macosx.x86_64_3*.jar too. part.

Exporting an applet as jar

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.

How can I call Java applet methods with Javascript?

i've created a java applet last year for a socket connection from a web application to a local running java server. it worked fine.
since the last java updates (7 r21 i guess), i cannot access the methods within javascript anymore. Right now, i reduced the applet to a test applet (without the doPriviligedAction methods) but even this does not work anymore.
The current code is like
import java.applet.*;
public class socketApplet extends Applet {
public void init() {
System.out.println("Applet initialisiert.");
}
public void start() {
System.out.println("Applet gestartet.");
}
public void paint() {
System.out.println("Applet aktualisiert.");
}
public void stop() {
System.out.println("Applet angehalten.");
}
public void destroy() {
System.out.println("Applet beendet.");
}
public String testApplet() {
System.out.println("Applet getestet.");
return "Yep, I'm the Applet.";
}
}
Before the update i could access methods like testApplet() in javascript like this:
document.socketApplet.testApplet();
The applet is self-signed and embedded with an applet html-tag. It is starting (the java console is opening and prints the debug messages defined in the init, start and paint methods) but i cannot access the testApplet() method. The response in Javascript is "undefined" while the applet exists.
after reading a while (a few days now...) about the new security changes, i've added a manifest.txt with the following content:
Main-Class: socketApplet
Permissions: all-permissions
Codebase: *
Trusted-Library: true
No luck with or without the Trusted-Library attribute.
What do i have to do to enable the access with javascript again?
Edit:
The implementation:
<applet id="socketApplet" width="100" height="100" archive="../../socketApplet.jar" name="socketApplet" code="socketApplet" scriptable="true">
I am testing with the newest versions of Firefox and Safari on an up to date Mac OS X machine.
Edit2:
i am creating and signing the jar like this
Edit3:
Okay, now my jar worked a few times (not in a row), i got
and in the console
But most of the time it does not work. restarting the browser, clearing caches, nothing works. going to test this on another pc now (again).
Edit4:
Okay it is running on a virtual machine with windows xp and java 32bit 7u25 - on my 64bit mac just only 1 out of 30 tries.
Okay i found the source of all evil...
it had nothing to do with the applet. the confusing situation was that it worked in firefox on windows and not in firefox on the mac (same FF versions, same java versions). Safari on my mac didn't work because the plugin was disabled...
So it was a Firefox on mac problem only. I've tested different situations and the applet worked when writing the applet code above into a html page. Before, i've created the applet dynamically (what is necessary in the software):
var applet = document.createElement('applet');
applet.archive = 'socketApplet.jar';
applet.id = 'socketApplet';
applet.name = 'socketApplet';
applet.code = 'socketApplet';
applet.scriptable = 'true';
applet.width = '0';
applet.height = '0';
document.body.appendChild(applet);
It works everywhere but not in Firefox on Mac. So as a workaround i have to embed the applet in an iframe and i have to embed the iframe dynamically. that works...
var mFrame = document.createElement('iframe');
mFrame.id = 'testFrame';
mFrame.height = '200';
mFrame.width = '400';
document.body.appendChild(mFrame);
mFrame.src = 'frame.html'; // contains the applet code
Next i am going to change the html tag to the object and embed tag for IE support. Thanks for your help Andrew Thompson!
I dislike the iframe version because the access to the applet via javascript is more complex, but it seems like there is no other way around.
I am going to submit a Mozilla bug ticket for this.
https://bugzilla.mozilla.org/show_bug.cgi?id=912880
Maybe similar to this one but another situation:
https://bugzilla.mozilla.org/show_bug.cgi?id=872969

method must call super() error in Netbeans

Recently I've made a Netbeans project and I am using SVN along with it. I am seeing duplicate class error, and in the console it says
java.lang.VerifyError: (class: pie/chart/explorer/PieChartExplorer, method: <init> signature: ()V) Constructor must call super() or this()
Could not find the main class: pie.chart.explorer.PieChartExplorer. Program will exit.
Exception in thread "main" Java Result: 1
Here is PieChartExplorer.java:
package pie.chart.explorer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class PieChartExplorer extends JFrame implements ActionListener {
JTextField one = new JTextField(10);
JTextField two = new JTextField(10);
JTextField three = new JTextField(10);
JButton sub = new JButton("Click to be amazed");
public PieChartExplorer() {
super("Pie Chart Explorer");
setSize(300,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);
setVisible(true);
add(one);
add(two);
add(three);
sub.addActionListener(this);;
add(sub);
}
public static void main(String[] args) {
PieChartExplorer app = new PieChartExplorer();
}
#Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == sub) {
try {
Pie show = new Pie(Float.parseFloat(one.getText()),Float.parseFloat(two.getText()),Float.parseFloat(three.getText()));
} catch(Exception ex) {
JOptionPane.showMessageDialog(this, "Please check entered data");
}
}
}
}
I have tried:
Clean and Rebuild project
Making sure that I have called super in all constructors
How can this be fixed? Code for download.
I found that renaming the package did not work, the old package was still there.
The problem for me started when I copied a package from another application into the current application, which already had a package with the same name. I was trying to add some missing classes to the package. After I did that, the error started.
To resolve it, I deleted the entire package from the target web app and did a clean and build. Then I copied the source package into the target application. No errors.
I saw these symptoms just the other day.
I had I file I had been editing and decided I wanted to split my changes into 2 commits. I went to the directory containing my file "x/y/Z.java", made a directory in "x/y" named "backup", moved "Z.java" there, and pulled a fresh copy from version control. Note all of this was done outside the IDE.
Back in the IDE I merged in the changes for the first commit and when I built I got the duplicate class message for "Z.java".
When I copied the source to "backup" I did it outside the IDE and it still had the original package "x.y" as did my newly edited "Z.java". NB would not compile the new "Z.java" because it could see it had already created "x.y.Z.class" (from "x/y/backup/Z.java").
There are 2 ways to fix this:
Rename "x/y/backup/Z.java" to "x/y/backup/Z.java.backup". (Prevent the backup copy from being compiled.)
Change the package in "x/y/backup/Z.java" from "x.y" to "x.y.backup". (Make the backup create a different class file.)
After making either of these changes, perform a "clean and build". Note: simply building will not fix the problem, you need to perform a clean to remove the rogue class file.
Note: #1 was done by renaming Z.java from the command line, not within NB. NB will not let you change the file extension.
Cleaning and Building solves the problem
If you still have the problem, this is how I solved it..
In my case I changed the class with main method later and the initial class was still referenced in the proporties file.
Change that setting, clean and build.. It worked for me...
In my case, i had the same problem in a Web application after making an external copy of a POJO and manually editing it outside NETBEANS. The problem actually was what the others suggested in other answers about a conflict in the already compiled .class files.
What i did to overcome this was simply delete the folder webAppname/WEB-INF/classes (where compiled classes reside) and then do a Clean and Build
Hope this helps someone

Categories