JApplet loading problem - java

I want to convert an java application to applet, but I an having problems to load it in the browser I presume this is because of the package.
package com.applet;
import java.applet.Applet;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
//import javax.swing.JOptionPane;
#SuppressWarnings("serial")
public class AppletDriver extends Applet {
//Called when this applet is loaded into the browser.
public void init() {
//Execute a job on the event-dispatching thread; creating this applet's GUI.
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
CleanerPanel cFrame = new CleanerPanel();
add(cFrame);
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
}
this is the code I am using to call the applet when I run it in Eclipse it is working.
this is the html code:
<applet archive="app.jar" code="bin/com/applet/AppletDriver.class" width=350 height=200>
</applet>
the app.jar is in the main dir of the eclipse project any suggestions ?
error from browser java console:
java.lang.NoClassDefFoundError: bin/com/applet/AppletDriver (wrong name: com/applet/AppletDriver)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.NoClassDefFoundError: bin/com/applet/AppletDriver (wrong name: com/applet/AppletDriver)

Remove the bin\ from the start of the code attribute value, and use / instead of \ (we're not in Washington anymore). If the jar is in a directory named bin then you'll need to use archive="bin/app.jar".
Actually looking at the stack trace, the jar has been constructed incorrectly. The class file should be in a directory com/applet, not bin/com/applet.

Related

How can I fix "javax.swing.ImageIcon.<init>(Unknown Source)"?

I'm making a simple program of a PONG game, and my problem comes from the Main method which i wanted to set an image icon and it shoots me a NullPointerException.
I'm working with Eclipse IDE 2019,06 and the Java Compiler 12.0.1. The image that I am using is .jpg.
I've tried to change the directory of the image and nothing :(.
The directory of the image is in a folder which is in the same folder of the main class.
This is my frame code:
public class Menu {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Menu window = new Menu();
window.frame.setVisible(true);
window.frame.setLocationRelativeTo(null);
window.frame.setTitle("PONG!");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Menu() {
frame.setIconImage(new ImageIcon(Menu.class.getResource("/icon.jpg")).getImage());
initialize();
}
So this is the error message:
java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at gui.Menu.<init>(Menu.java:57)
at gui.Menu$1.run(Menu.java:42)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I hope you can help me with this problem. Thanks.
I guess line 57 of Menu.java is this line:
frame.setIconImage(new ImageIcon(Menu.class.getResource("/icon.jpg")).getImage());
It looks like frame has not been initialized and therefore is null. That's why you get a NullPointerException.
When you work with the Maven build system (e.g. netbeans 11.3, maven 3.3.x), you have to take care where you place your resources. The "/icon.jpg" path is relative to your resources directory. Try to create a directory named "resources" (sic!) as a subdirectory of the java main source directory. For example, the path to "resources" should be
<Project root>/src/main/resources
You place your image files in this directory or a maybe a subdirectory /src/main/resources/icons/icon.jpg

Swing apps doesn't run

I have a problem in using swing. I don't know what is the cause of this but i'm just trying to create a simple frame app using swing at it give me a lot of error.
import javax.swing.JFrame;
public class StacksGui {
public static void main(String args[])
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(100,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Exception in thread "main" java.lang.ExceptionInInitializerError
at javax.swing.JPanel.updateUI(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JRootPane.createGlassPane(Unknown Source)
at javax.swing.JRootPane.<init>(Unknown Source)
at javax.swing.JFrame.createRootPane(Unknown Source)
at javax.swing.JFrame.frameInit(Unknown Source)
at javax.swing.JFrame.<init>(Unknown Source)
at StacksGui.main(StacksGui.java:9)
Caused by: java.lang.IllegalArgumentException: 0 incompatible with Text-specific LCD contrast key
at java.awt.RenderingHints.put(Unknown Source)
at sun.awt.windows.WDesktopProperties.getDesktopAAHints(Unknown Source)
at sun.awt.windows.WToolkit.getDesktopAAHints(Unknown Source)
at sun.awt.SunToolkit.getDesktopFontHints(Unknown Source)
at sun.awt.windows.WDesktopProperties.getProperties(Unknown Source)
at sun.awt.windows.WToolkit.updateProperties(Unknown Source)
at sun.awt.windows.WToolkit.lazilyInitWProps(Unknown Source)
at sun.awt.windows.WToolkit.lazilyLoadDesktopProperty(Unknown Source)
at java.awt.Toolkit.getDesktopProperty(Unknown Source)
at javax.swing.UIManager.<clinit>(Unknown Source)
... 10 more
I had problem when all java Swing apps crashed silently.
After trying to launch from command line got the following exception:
Caused by: java.lang.IllegalArgumentException: 181193932 incompatible with Text-specific LCD contrast key
As it's said in JDK-6503988, the problem is connected with ClearType registry setting FontSmoothingGamma.
This can be fixed by running "Adjust ClearType text" from Control panel (cttune.exe) or by changing the registry directly:
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /t REG_DWORD /v FontSmoothingGamma /d 1024 /f
A reboot may be needed for changes to take the effect.
See also a 10-year-old investigation of the same problem at https://www.rarst.net/software/cleartype-install4j-java-bug/.
This code works for me:
package test;
import javax.swing.JFrame;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(100,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I only see a difference at
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
where I added the frame. However your stacktrace looks like there's another error, so first try this and if it still don't work, give us a bit more code to look at.

Protege-OWL: java.lang.NoClassDefFoundError

Im trying to run code use protege-owl. So I added the protege.jar and protege-owl.jar to library to import edu.stanford.smi.protegex.owl.* and edu.stanford.smi.protege.*.
I also added jena jar files to use in next steps.
First i wrote simple code to see how it works :
import java.lang.Object;
import edu.stanford.smi.protege.*;
import edu.stanford.smi.protegex.owl.*;
import edu.stanford.smi.protegex.owl.jena.JenaOWLModel;
import edu.stanford.smi.protegex.owl.model.OWLNamedClass;
public class Main {
public static void main(String[] args) {
// TODO code application logic here
JenaOWLModel model=ProtegeOWL.createJenaOWLModel();
OWLNamedClass c= model.createOWLNamedClass("A");
System.out.print(c.getName());
}
}
But it gives me this error :
CONFIG: Protege 3.0 Build 141, JVM 1.6.0_20-b02, memory=259M, Windows 7, encoding=UTF-8, language=fa, country=IR
WARNING: Look and feel not found: com.jgoodies.plaf.plastic.PlasticLookAndFeel -- SystemUtilities.loadLookAndFeel()
WARNING: Plugins directory not found: C:\Users\mona\Documents\NetBeansProjects\JavaApplication3\plugins -- PluginUtilities.getPluginsDir()
WARNING: Exception Caught -- java.lang.NoClassDefFoundError: com/toedter/calendar/JDateChooser
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at edu.stanford.smi.protege.plugin.PluginUtilities.isLoadableClass(Unknown Source)
at edu.stanford.smi.protege.plugin.PluginUtilities.checkPlugin(Unknown Source)
at edu.stanford.smi.protege.plugin.PluginUtilities.checkPlugins(Unknown Source)
at edu.stanford.smi.protege.plugin.PluginUtilities.processManifest(Unknown Source)
at edu.stanford.smi.protege.plugin.PluginUtilities.loadPluginsWithClassLoader(Unknown Source)
at edu.stanford.smi.protege.plugin.PluginUtilities.createClassLoaderAndLoadPlugins(Unknown Source)
at edu.stanford.smi.protege.plugin.PluginUtilities.loadPlugins(Unknown Source)
at edu.stanford.smi.protege.plugin.PluginUtilities.loadSystemPlugins(Unknown Source)
at edu.stanford.smi.protege.plugin.PluginUtilities.loadPlugins(Unknown Source)
at edu.stanford.smi.protege.plugin.PluginUtilities.init(Unknown Source)
at edu.stanford.smi.protege.plugin.PluginUtilities.<clinit>(Unknown Source)
at edu.stanford.smi.protege.util.SystemUtilities.init(Unknown Source)
at edu.stanford.smi.protege.util.SystemUtilities.<clinit>(Unknown Source)
at edu.stanford.smi.protege.model.Project.<clinit>(Unknown Source)
at edu.stanford.smi.protegex.owl.ProtegeOWL.createJenaOWLModel(ProtegeOWL.java:32)
at javaapplication3.Main.main(Main.java:25)
Caused by: java.lang.ClassNotFoundException: com.toedter.calendar.JDateChooser
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method) . . . .
What can I do ?
Update:
I replace protege.jar with protege-3.5.jar and now it gives :
Exception in thread "main" java.lang.NoSuchMethodError: edu.stanford.smi.protege.model.SystemFrames.replaceFrame(Ledu/stanford/smi/protege/model/Frame;)V
at edu.stanford.smi.protegex.owl.model.impl.AbstractOWLModel.createSystemFrames(AbstractOWLModel.java:544)
at edu.stanford.smi.protege.model.DefaultKnowledgeBase.<init>(DefaultKnowledgeBase.java:79)
at edu.stanford.smi.protegex.owl.model.impl.AbstractOWLModel.<init>(AbstractOWLModel.java:318)
at edu.stanford.smi.protegex.owl.jena.JenaOWLModel.<init>(JenaOWLModel.java:62)
at edu.stanford.smi.protegex.owl.jena.JenaKnowledgeBaseFactory.createKnowledgeBase(JenaKnowledgeBaseFactory.java:55)
at edu.stanford.smi.protege.model.Project.createDomainKB(Project.java:429)
at edu.stanford.smi.protege.model.Project.createDomainKnowledgeBase(Project.java:447)
at edu.stanford.smi.protege.model.Project.<init>(Project.java:359)
at edu.stanford.smi.protege.model.Project.<init>(Project.java:341)
at edu.stanford.smi.protege.model.Project.createNewProject(Project.java:545)
at edu.stanford.smi.protegex.owl.ProtegeOWL.createJenaOWLModel(ProtegeOWL.java:32)
at javaapplication3.Main.main(Main.java:24)
Java Result: 1
Where is the problem?
I downloaded protege-owl.jar which is 3.24MB from different link and now it works.

"Prohibited package name: java.util" on Class.forName("java.util...") in applet

I have an applet that references 2 signed jars:
myapplet.jar
jackson-all-1.9.9.jar
When starting the applet the second time (first time is without errors), I get this:
Exception in thread "thread applet-main.MyApplet-1"
java.lang.ExceptionInInitializerError
at org.codehaus.jackson.map.deser.StdDeserializerProvider.<init>(StdDeserializerProvider.java:81)
at org.codehaus.jackson.map.ObjectMapper.<init>(ObjectMapper.java:398)
at org.codehaus.jackson.map.ObjectMapper.<init>(ObjectMapper.java:358)
at org.codehaus.jackson.map.ObjectMapper.<init>(ObjectMapper.java:328)
at net.Remote.<init>(Remote.java:50)
at main.Env.init(Env.java:44)
at main.MyApplet.init(MyApplet.java:25)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.SecurityException: Prohibited package name: java.util
at java.lang.ClassLoader.preDefineClass(Unknown Source)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.codehaus.jackson.map.deser.BasicDeserializerFactory.<clinit>(BasicDeserializerFactory.java:74)
... 9 more
The line in question is the first one in the following try-catch block:
try {
Class<?> key = Class.forName("java.util.ConcurrentNavigableMap");
Class<?> value = Class.forName("java.util.ConcurrentSkipListMap");
#SuppressWarnings("unchecked")
Class<? extends Map<?,?>> mapValue = (Class<? extends Map<?,?>>) value;
_mapFallbacks.put(key.getName(), mapValue);
} catch (ClassNotFoundException cnfe) { // occurs on 1.5
}
A couple of things I do not understand:
Why does my Java7 JVM not take it out of its runtime library? But rather
Why does it try to download /java/util/ConcurrentNavigableMap.class from my server, which obviously fails with a 404?
As that fails, why does it try to re-download myapplet.jar 25 times in rapid succession, each time successfully (200), and each time returning the same jar file?
Update I'm not sure whether the 25 retries are caused by the class loader trying to load the class, it might be some other code trying to load a resource (which would still be odd, but not related to the CurrentNavigableMap issue), so I'll exclude that from my question.
N.B. I guess it does not try to re-download the jackson jar file, as that one is listed in the cache_archive attribute.
Is this?
wrong:
Class.forName("java.util.ConcurrentNavigableMap");
Correct:
http://java.sun.com/javase/ja/6/docs/ja/api/java/util/concurrent/package-tree.html
Class.forName("java.util.concurrent.ConcurrentNavigableMap");

How to call a sighned Applet which returns a value from GWT on button click

I am trying to call an sighned applet on by GWT button click. I tried going through the gwtai method of creating an interface on client side then writing an Applet which implements the interface.Howevere I get a TargetInvokationException when I call the method on my onClick() event.
Here is the code I wrote :
screenCap.addListener(new ButtonListenerAdapter() {
public void onClick(Button button, EventObject e) {
String filePath = counterApplet.captureScreenShot(); (Throws exception here)
TextField filePathT = new TextField();
My Applet Interface :
import com.google.gwt.gwtai.applet.client.*;
#ImplementingClass(com.jpmorgan.rd.querymanager.applet.imageCaptureAppletImpl.class)
#Height("10")
#Width("10")
#Archive("GwtAI-Client.jar,GwtAI-Demo.jar")
public interface imageCaptureApplet extends Applet {
String captureScreenShot();
}
My applet implementation class :
import java.awt.*;
import javax.swing.JApplet;
public class imageCaptureAppletImpl extends JApplet implements imageCaptureApplet{
private static final long serialVersionUID = -511774647966368672L;
public String captureScreenShot() {
String file;
// do something
return file;
}
And the full stack trace:
load: class com.amol.test.imageCaptureAppletImpl.class not found.
java.lang.ClassNotFoundException: com.jpmorgan.rd.querymanager.server.imageCaptureAppletImpl.class
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: open HTTP connection failed:http://127.0.0.1:8888/com/amol/test/imageCaptureAppletImpl/class.class
at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 9 more
Exception: java.lang.ClassNotFoundException: com.amol.test.imageCaptureAppletImpl.class
Any help around this is Appreciated.
I have also created a signed jar of my applet can you please suggest if there is a better way of calling this captureScreenShot() method from my GWT page on button click and getting a result back.
Also is JSNI a better way of doing this. If so can you show a example where a applet which passes a value back is called.
Many thanks in advanced.
PS: I used this page for the basis of my code on gwtai :
http://code.google.com/p/gwtai/wiki/GettingStarted
Thanks,
Amol
GwtAI is using JSNI to communicate with the applet. So whether you are writing the JSNI code yourself or using this library will be the same. Can you post the full stacktrace of your error?

Categories