Why do I get NoClassDefFoundError: java/awt/Desktop? - java

I'm trying to open an URI with Swing that I get above error.
What is the reason and how can I fix it?
When I do it in console everything is OK but when I do in GUI I get this error.
I should say that I use Weblogic as server.
Code
private static void open(URI uri) {
if (Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(uri);
} catch (IOException e) { /* TODO: error handling */ }
} else { /* TODO: error handling */ }
}
Stack trace:
Exception in thread "AWT-EventQueue-1" java.lang.NoClassDefFoundError: java/awt/Desktop
at be.azvub.ext.bcfidownloder.BcfiDownloadPanel.open(BcfiDownloadPanel.java:230)
at be.azvub.ext.bcfidownloder.BcfiDownloadPanel.access$000(BcfiDownloadPanel.java:37)
at be.azvub.ext.bcfidownloder.BcfiDownloadPanel$7.actionPerformed(BcfiDownloadPanel.java:147)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:5517)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3129)
at java.awt.Component.processEvent(Component.java:5282)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3984)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3819)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1791)

Doc on NoClassDefFoundError
The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.
You do have some incorrect classloading happening. Mostly due to wrong class loader chaining.

NoClassDefFoundError can only be caused by a classpath problem.
Because Desktop is part of jre, make sure that your classpath contains a reference to the jre library.
In Eclipse, you can go to run configurations --> Classpath and check there
UPDATE:
As Andrew suggested, you can also check you have at least java 1.6

java.awt.Desktop has been introduced in Java 6. Chances are high you're running your code on different JRE versions.

Related

NoSuchMethodError: com.google.common.cache.CacheBuilder.maximumSize(J)

I seem to be having a problem which only applied to 1 fellow user of a minecraft plugin of mine.
[15:54:14 ERROR]: Error occurred while enabling <Plugin> v1.0.8 (Is it up to date?)
java.lang.NoSuchMethodError: com.google.common.cache.CacheBuilder.maximumSize(J)Lcom/google/common/cache/CacheBuilder;
Is there any reason as to why this is happening or if I can over come it some way?
My code:
private LoadingCache<String, String> profileCache = CacheBuilder.newBuilder().
maximumSize(500).
expireAfterWrite(4, TimeUnit.HOURS).
build(new CacheLoader<String, String>() {
public String load(String name) {
try {
return getProfileJson(name);
} catch (IOException e) {
Bukkit.getLogger().info("Error, " + e.getLocalizedMessage() + ".");
}
return null;
}
});
You need to include the libraries into the exported jar. By default they are not included in the exported Jar as the IDE assumes they are present at runtime, which they are not.
See here for Gradle and here for Maven.
java.lang.NoSuchMethodError is thrown at runtime because the JVM does not find the method in the referenced class. This typically happens because you are using different versions of a third party library for compiling and running the application.
Check the version of the library used for compiling and the version used for running the code and make sure they match or are at least compatible.

java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: java.lang.ClassNotFoundException: ServicesTableau

I need your help around JAVA RMI, i developped a sample program used to sort table. but i got this exception:
Erreur RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: ServicesTableau
and this is my Server source code :
public class Serveur {
public static void main(String args[]) {
try {
System.out.println("Server start ...");
ServicesTableauImpl od = new ServicesTableauImpl();
String url = "rmi://" + args[0] + "/ServicesTableauImpl";
System.out.println("Passe");
Naming.rebind(url, od);
System.out.println("Attente d'invocations de client / CTRL-C pour stopper");
} catch (Exception e) {
System.out.println("Erreur " + e.getMessage());
}
/*
catch(java.net.MalformatedURLException e){
System.out.println("Mauvais nom de serveur");
System.exit(1);
}
catch(RemoteException e){
System.out.println("Pas de Rmiregistry");
System.exit(1);
}
*/
}
}
That class isn't available on the CLASSPATH of the RMI Registry. The simplest way to fix it is to start the Registry in the same JVM, via LocateRegistry.createRegistry(). Store the result in a static field.
This happened to me because I didn't run rmiregistry in the same directory as the server.
I spent a few hours to successfully start the simple example form Orcale!
Hope could help you
First, I run the program on Windows
I totally copy the code from here
but i delete the package and put that three .class file together for the convince
and if you are not familar with this i hope you'd better do it, too
because when package get in, the problem will be more complicated
There are three steps to do:
1.start rmiregistry
start rmiregistry -J-Djava.class.path=./
it is for "remote interface definition"
so that the rmiregistry can find the class
then solve the ClassNotFoundException
2.run server
start java Server
then you can see the output:
Server ready
3.run client
java Client
output:
response: Hello, world!
then talk about how to deal with package
Usually the Server.class and interface.class---"remote interface definition"
should be in the same package
and you need to know how to run the .class with package
java {packagename}.Server
the classpath default is "./"
and package declare will help to locate the interface.class which is needed for Server.class
so we don't need to separately set -cp
BTW, if you try set the real classpath, you will get error instead
As for "start rmiregistry"
it has no default classpath
so we need to set it
and it should be the same as the classpath's default value "./"
In some cases, you need to move you rmi clients code (interface and main) to default package. Repeate it for server side. It can solve you problem(work with packaging in rmi is not so obvious).
The answer is for IDE based project.
you need to 'start rmiregistry' from with in the build folder.
for example:
in Intellij
open terminal inside 'target -> classes' folder
run 'start rmiregistry'
then run your server code using intellij
for eclipse I hope its build folder
This error is because of not writing security manager code, because of which the class can't get loaded.
So make sure you add code for the security manager.
If the compiler doesn't find the security manager then create a new one.Add the code in main method.
if (System.getSecurityManager() == null) then
System.setSecurityManager(new RMISecurityManager())

#throws in Scala does not allow calling Java to catch correct exception type

I have some Scala code like this:
class Callee {
#throws(classOf[MyCheckedException])
def doStuff() {
}
}
Calling it from Java like so:
public class Caller {
public static void main(String[] args) {
// this won't compile; the Java compiler complains that the catch block is unreachable
// however without the catch block, it complains "unhandled exception MyCheckedException"
try {
new Callee().doStuff();
}
catch (MyCheckedException e) {
}
}
}
Removing the catch block results in an error from the Java compiler saying 'unhandled exception type MyCheckedException'. Adding the catch block for MyCheckedException results in the compiler complaining about the catch block being unreachable, because the exception is never thrown.
If I catch Exception and do an instanceOf, I can trap the correct exception coming out of doStuff, but I thought the #throws annotation was supposed to generate the right bytecode for the proper catch block to work. Am I wrong, or is there a bug here?
For the record, this is with Scala 2.9.2 and Java 1.6.
Edit: It compiles fine invoking javac/scalac using sbt from the command line. The error is only apparent during compile-as-you-type in Eclipse, which suggests the bug is in either the Eclipse Java Compiler or some part of the IDE. Can others reproduce it this way? I am using Eclipse 3.7.2
I can reproduce this on Helios with 2.9.1. It is a bug in the presentation compiler, and you should raise it as a bug on http://www.assembla.com/spaces/scala-ide/tickets.
For future reference, this issue has been fixed (https://github.com/scala-ide/scala-ide/commit/055a81cd3fe792e4327668791888c30cf04793f5). The fix is already available with both Scala IDE 2.0.x and Helium nightlies.
Furthermore, it will be included in the next Scala IDE 2.0.2 maintenace release.
(sorry for the additional noise, but I realized that having an answer was more visible than a simple comment)

How to use kyotocabinet(JNI) in playframework?

I'm tackling to use kyotocabinet in Playframework.
and following error occurred.
I'm using Eclipse and playframework-1.2.3.
and kyotocabinet is native library so I'm using its Java-Binding.
the reproduce code is simple.
in controller:
public static void somePage() {
DB db = new DB();//error occurred
render();
}
Internal Server Error (500) for request GET /
Execution exception (In /app/controllers/TestApp.java around line 45)
NoClassDefFoundError occured : Could not initialize class kyotocabinet.DB
play.exceptions.JavaExecutionException: Could not initialize class kyotocabinet.DB
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:229)
at Invocation.HTTP Request(Play!)
Caused by: java.lang.NoClassDefFoundError: Could not initialize class kyotocabinet.DB
at controllers.TestApp.somePage(TestApp.java:45)
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:546)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:500)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:476)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:471)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:159)
... 1 more
build in Eclipse was completed but error occured at runtime.
I guess it is because kyotocabinet.dll is missing.(only jkyotocabinet.jar was found)
I configured the location of kyotocabinet.dll to Java Build Path > Source > Native Library Location of my playframework project.
and it was good in other projects.
How to use native library in playframework?
any example or tutorial?
Play.getFile and System.load didn't work.
package controllers;
import play.Play;
import play.jobs.*;
#OnApplicationStart
public class Bootstrap extends Job {
public void doJob() {
String path = "D:/MyProject/lib/jkyotocabinet.dll";
Play.getFile(path);
//System.load(path); if this was enabled, following error occurred: "Native Library D:\MyProject\lib\jkyotocabinet.dll already loaded in another classloader". so I guess the dll was loaded.
System.out.println("bootstrap loaded");//this is displayed.
}
}
UnsatisfiedLinkError occured : no jkyotocabinet in java.library.path
this Japanese blog tells Play!Framework cannot load native library.
http://d.hatena.ne.jp/hjym_u/20110702/1309609277
I already tried these: Absolute path, Relative path, System.load, System.loadLibrary, Play.getFile.
as conclusive approach, I put jkyotocabinet.dll to current directory(D:/MyProejct/), and wrote this code.
public static void somePage(){
File f = Play.getFile("jkyotocabinet.dll");
if(f != null && f.isFile() && f.canRead() && f.canExecute()){//true
DB db = new DB();//error occured. it reached here.
}
render();
}
Execution exception
NoClassDefFoundError occured : Could not initialize class kyotocabinet.DB
Play.getFile found the path "jkyotocabinet.dll" so jkyotocabinet.dll is in current directory so jvm should find it automatically.
anyone can use JNI in playframework?
finally, I could use kyotocabinet as PROD mode but not DEV mode.
Project/conf/application.conf
#application.mode=dev
application.mode=prod
I assume you just need to load the dll into Java via System.load
If you place the ddl on your project, you may load it via Play.getFile inside your #OnApplicationStart controller. This should make it available to your application while the JVM is alive.
EDIT:
#KenichiYamamoto Play.getFile gets files from the application path. You are trying to use a full path in there.
Read this about loading the file in a container. It may be that (due to Play compile-reload system) you are hitting the "already loaded" error. Try to follow the example by adding the System.load inside a static block in your #OnApplicationStart
Do as pere says but use the relative path from your application root. Not the absolute path. I.e. Play.getFile("lib\myfile.dll")

Another odd NoClassDefFoundError on WhiteSpaceProcessor

I'm strugelling with an odd problem for days now.
Only one of the users of my webapp get an NoClassDefFoundError when trying to use some functionallity. This is the stacktrace:
java.lang.NoClassDefFoundError: com/sun/xml/bind/WhiteSpaceProcessor
at com.sun.xml.bind.DatatypeConverterImpl._parseInt(DatatypeConverterImpl.java:105)
at com.foo.bar.webservice.generated.GetLoginsRequest_JaxbXducedAccessor_panelId.parse(TransducedAccessor_field_Integer.java:32)
at com.sun.xml.bind.v2.runtime.unmarshaller.StructureLoader.startElement(StructureLoader.java:166)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:406)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:384)
at com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:35)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:101)
at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:224)
at com.sun.xml.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:107)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:289)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:272)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:106)
at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:424)
On a strange way WhiteSpaceProcessor can't be found while it is on the classpath.
I used tattletale to look at the possitions of the usage of the classes:
WhiteSpaceProcessor only exist once on the classpath:
DatatypeConverterImpl only exist once on the classpath
I'm stuck on the fact that the exact war on a different environment is working perfect.
working environment:
Windows machine
Tomcat 5.5.28
Java 5 (jdk1.5.0.22)
none working environment:
Linux machine
Tomcat 5.5.??
Java 5 (jdk1.5.0.22)
I hope somebody can sent me in the right direction.
tomcat server is already restarted
Did you use tattletale on the working or non-working machine?
Perhaps the failing environment contains some jar file in jre/lib/ext (or a similar extensions directory), and that's being used in preference to a "lower down" version?
EDIT: Just to go into a bit more detail about the situations in which NoClassDefFoundError can be thrown, it's worth reading the JVM spec, chapter 5. It talks about three situations:
The resource corresponding to the class can't be found at all
The resource is found, but doesn't correspond to the right class (although in that case I'd expect a message including "wrong name")
You're using a version of Java earlier than 1.2, and the class file has an unsupported major/minor version number. (This situation now throws UnsupportedClassVersionError.)
Also read section 2.17.5: it states that if the class is in an "erroneous state" (e.g. previously initialization failed, or there was a bytecode verification failure) then NoClassDefFoundError will be thrown.
Now, if the static initializer of the class fails then the first caller sees an ExceptionInInitializerError - but the second caller sees NoClassDefFoundError. Here's a short but complete program to demontrate this:
class Foo {
static {
if (true) {
throw new RuntimeException();
}
}
static void foo() {
}
}
public class Test {
public static void main(String[] args) {
try {
Foo.foo();
} catch (Throwable t) {
System.out.println("First exception: " + t);
}
try {
Foo.foo();
} catch (Throwable t) {
System.out.println("Second exception: " + t);
}
}
}
Now unless something in your system is suppressing the ExceptionInInitializerError, I'd expect to see that in the log before NoClassDefFoundError if that were the problem. I still think it's more likely that your failing system is loading one class in an extension classloader which then can't find the ShiteSpaceProcessor class.
NoClassDefFoundError does not mean that the class file cannot be found in the classpath. It means that the class cannot be loaded. This is generally due to an error during initialization, or, more often, a version mismatch in JAR files on which the class depends.
Eg, you probably compiled against XYZ package version 1.2 and your user has XYZ version 1.1 installed.

Categories

Resources