Strange class files appearing in my bin folder - java

I am using Eclipse + Window Builder + few third party libraries to build a gui application,
Entry point for my application resides in MainWindow.java file, which also contains the gui of the application, other than that I have few helper classes.
My application works fine, but when I checked into bin folder I find four more class files there namely:
1. MainWindow$1.class
2. MainWindow$2.class
3. MainWindow$3.class
4. MainWindow$4.class.
Now I don't understand why these files are here, When I deleted these files, Eclipse throws an exception
Exception in thread "main" java.lang.NoClassDefFoundError: gui/MainWindow$1
at gui.MainWindow.main(MainWindow.java:71)
Caused by: java.lang.ClassNotFoundException: gui.MainWindow$1
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
the code that throws the exception is:
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frmMailExtractor.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
I am not able to find out why these files are there and who put them? any ideas?
OK so these are the anonymous classes, but why the Exception then?...and why can't I see them in my eclipse..becasue when I export it as a runnable jar, the jar throws the same exception

These are class files generated for anonymous inner classes inside MainWindow. It probably contains a bunch of event listeners implemented as such.
Anonymous inner classes get compiled into separate class files with the name <OuterClass>$<nnn>.class, where <nnn> is a compiler-generated number.

Those are what anonymous classes get compiled into. The number in the filename is the ordinal of the related anonymous class in the respective Java compilation unit.

Related

java.lang.NoClassDefFoundError when Translating

I am using the Apertium Translator and using the sample code they provide. My code is like this.
import com.robtheis.aptr.language.Language;
import com.robtheis.aptr.translate.Translate;
public class Test {
public static void main(String[] args) throws Exception {
// Set the Apertium API Key - Get yours at http://api.apertium.org/register.jsp
Translate.setKey("BNSCFhEL8DoTApc2I1+aa3UYkVg");
String translatedText = Translate.execute("Hola, mundo!", Language.SPANISH, Language.ENGLISH);
System.out.println(translatedText);
}
}
I have no errors or warnings and when I run the program I get the following errors.
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONValue
at com.robtheis.aptr.ApertiumTranslatorAPI.jsonSubObjToString(ApertiumTranslatorAPI.java:195)
at com.robtheis.aptr.ApertiumTranslatorAPI.retrieveSubObjString(ApertiumTranslatorAPI.java:140)
at com.robtheis.aptr.translate.Translate.execute(Translate.java:56)
at maple.Test.main(Test.java:11)
Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONValue
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 4 more
The .jar I'm using is the second one from https://github.com/rmtheis/apertium-translator-java-api/downloads
You have to download the first one. The first jar file (apertium-translator-java-api-0.2-jar-with-dependencies.jar) contains the all dependencies needed..
Or you add a json library to your project path..
Well JVM is not able to find some class at Runtime which was available at compile time. That is the reason of NoClassDefFoundError
Below you also have ClassNotFoundException for this class org.json.simple.JSONValue
which means you are trying to load the particular class at run-time by name.
Both of them related to Java Class Path.
I don't know alot about jSON stuff. But you are missing this file org.json.simple.JSONValue you can take it from here http://code.google.com/p/json-simple/
add the above jar file in your class path . And the code will definitely run. Guaranteed.!!!
Thanks

ClassNotFound exception in java command

I have simple Hello word program. Program will compile and run when not declare namespace in code but when I declared the class within namespace and compile the program it will complie successfully but it will given an error classnotfound when this program run. My question is why java complier not availble to find class when declare namespace (package) in code?
Please find below source code:
package org;
public class Chunk
{
public static void main(String [] args)
{
System.out.println("Hello, World");
}
}
and command is
java org.Chunk
and error is
java.lang.NoClassDefFoundError: org/Chunk
Caused by: java.lang.ClassNotFoundException: org.Chunk
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: org.Chunk. Program will exit.
Exception in thread "main"
You are getting this error probably because the class is not part of the classpath. You can specify a classpath using -cp java option, to point to the directory/jar where the org.Chunk class is found.

NoClassDefFoundError running java from the console

When I tried to run Java on Linux on the terminal this is what happens:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp/class
Caused by: java.lang.ClassNotFoundException: HelloWorldApp.class
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Can anyone help me with this?
EDIT: I was in the folder of the bytecode file and ran this on the terminal:
bash-4.1$ java class HelloWorldApp
Source file:
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
You are executing
java HelloWorldApp.class
but it must be
java HelloWorldApp
You may not append class to your call but name plain the classname.
Also, as others has remarked, it is better to use packages as classes in the default package do not work fine.
You also should note that if you have the package declaration in the code it will screw it up even if you try all the fancy fixes like setting CLASSPATH. For example if you have:
package blah;
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}
The line: package blah;
will cause java HelloWorld to fail after compiling. So remove this line and you should be able to run the src via cmd line.

Scanner no class error

I'm new to java and i have a problem while trying to run my program. I'm using eclipse.
import java.util.Scanner;
public class Scan {
public static void main(String[] args) {
String imie;
Scanner odczyt = new Scanner(System.in);
imie=odczyt.nextLine();
System.out.println("Witaj "+imie);
}}
This what I get:
EDIT(Runned without dot)
Exception in thread "main" java.lang.NoClassDefFoundError: Scan
Caused by: java.lang.ClassNotFoundException: Scan
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Scan. Program will exit.
Please help me. :)
When you're running java, don't put .java on the end of the class name.
It looks like you're trying to run a class named Scan.java, but there is no such class; the class is just named Scan. However you're launching your class, you need to launch just Scan, i.e.,
java Scan
not
java Scan.java
You need to specify classpath that specifies where Scan class can be found. The classpath can point to a folder of classes or a JAR file. For instance...
java -cp scan.jar Scan

How can I use Akka Actors in a Java application?

I would like to use Akka actors in Java.
I downloaded the akka-1.0.zip and added akka-actor-1.0.jar to my "Build Path" in Eclipse.
Then I wrote this Actor class:
package com.example;
import akka.actor.UntypedActor;
public class MyActor extends UntypedActor {
public void onReceive(Object message) throws IllegalArgumentException {
if (message instanceof String) {
System.out.println("Received: " + message);
} else throw new IllegalArgumentException("Unknown message: " + message);
}
}
But I get errors in Eclipse:
The type scala.Option cannot be resolved.
The type scala.Some cannot be resolved.
The type scala.PartialFunction cannot be resolved.
The type scala.ScalaObject cannot be resoled.
Do I need to add any more files to my "Build Path" or what am I doing wrong? I don't find the documentation beeing that helpful.
Update: I added scala-library.jar to my Build Path and the above erros disappeared. But I get an error when I compile and run the application:
Exception in thread "main" java.lang.NoClassDefFoundError: net/lag/configgy/ConfigMap
at akka.actor.Actors.actorOf(Actors.java:70)
at com.example.ActorTest.main(ActorTest.java:9)
Caused by: java.lang.ClassNotFoundException: net.lag.configgy.ConfigMap
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
Here is the main class where I use my actor:
package com.example;
import akka.actor.ActorRef;
import akka.actor.Actors;
public class ActorTest {
public static void main(String[] args) {
ActorRef myActor = Actors.actorOf(MyActor.class);
myActor.start();
System.out.println("My Actor started");
}
}
In your akka-1.0.zip file there is scala-library.jar. Try adding it to the build path.
Also, there is a lib_managed directory inside the zip, which contains further library files. Possibly aslo some of them will be needed.
To avoid this kind of situations you should try maven. There is a Akka repository: http://scalablesolutions.se/akka/repository/se/scalablesolutions/akka/
You can find a complete, working example here; it's a Maven project, so it will get the dependencies for you automatically.
I see this problem using Akka in a Java project in Eclipse with the Scala IDE installed. The problem goes away when using an Eclipse instance without the Scala IDE installed. Eclipse is perhaps confused by two Scala libraries around?
I don't know Akka actors but it seems you need some scala support (libs on the build path) as well.

Categories