NoClassDefFoundError: wrong name - java

I wrote a java program to test RESTful web services by using Netbeans7.0.1 and it works fine there. Now I wrote the build.xml file to compile the code and when I try to run the generated .class file I always got this exception:
Exception in thread "main" java.lang.NoClassDefFoundError: ClientREST (wrong name: clientrest/ClientREST)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: ClientREST. Program will exit.
The name and path are correct, so any thoughts why I'm getting this exception?

Exception in thread "main" java.lang.NoClassDefFoundError: ClientREST
So, you ran it as java ClientREST. It's expecting a ClientREST.class without any package.
(wrong name: clientrest/ClientREST)
Hey, the class is trying to tell you that it has a package clientrest;. You need to run it from the package root on. Go one folder up so that you're in the folder which in turn contains the clientrest folder representing the package and then execute java clientrest.ClientREST.
You should not go inside the clientrest package folder and execute java ClientREST.

I encountered this error using command line java:
java -cp stuff/src/mypackage Test
where Test.java resides in the package mypackage.
Instead, you need to set the classpath -cp to the base folder, in this case, src, then prepend the package to the file name.
So it will end up looking like this:
java -cp stuff/src mypackage.Test

To further note on Garry's reply: The class path is the base directory where the class itself resides. So if the class file is here -
/home/person/javastuff/classes/package1/subpackage/javaThing.class
You would need to reference the class path as follows:
/home/person/javastuff/classes
So to run from the command line, the full command would be -
java -cp /home/person/javastuff/classes package1/subpackage/javaThing
i.e. the template for the above is
java_executable -cp classpath the_class_itself_within_the_class_path
That's how I finally got mine to work without having the class path in the environment

Probably the location you are generating your classes in doesnt exists on the class path. While running use the jvm arg -verbose while running and check the log whether the class is being loaded or not.
The output will also give you clue as to where the clasess are being loaded from, make sure that your class files are present in that location.

Try the below syntax:
Suppose java File resides here: fm/src/com/gsd/FileName.java
So you can run using the below syntax:
(Make current directory to 'fm')
java src.com.gsd.FileName

Suppose you have class A
and a class B
public class A{
public static void main(String[] args){
....
.....
//creating classB object
new classB();
}
}
class B{
}
this issue can be resolved by moving class B inside of class A and using static keyword
public class A{
public static void main(String[] args){
....
.....
//creating class B
new classB();
static class B{
}
}

Here is my class structure
package org.handson.basics;
public class WithoutMain {
public static void main() {
System.out.println("With main()...");
}
}
To compile this program, I had to use absolute path. So from src/main/java I ran:
javac org/handson/basics/WithoutMain.java
Initially I tried with the below command from basics folder and it didn't work
basics % java WithoutMain
Error: Could not find or load main class WithoutMain
Caused by: java.lang.NoClassDefFoundError: org/handson/basics/WithoutMain (wrong name: WithoutMain)
Later I went back to src\main\java folder and ran the class with relevant package structure, which worked as expected.
java % java org.handson.basics.WithoutMain
With main()...

I also have encountered this error on Windows when using Class.forName() where the class name I use is correct except for case.
My guess is that Java is able to find the file at the path (because Windows paths are case-insensitive) but the parsed class's name does not match the name given to Class.forName().
Fixing the case in the class name argument fixed the error.

Related

I am trying to overload the main method but I am getting following error in cmd

filename: mainoverloading.java
error: could not find or load main class mainoverloading
class simple{
public static void main(int a)
{
System.out.println(a);
}
public static void main(String args[])
{
System.out.println("Hi");
main(10);
}
}
Your class is named simple (not mainoverloading). Rename the class (or move the file "mainoverloading.java" to "simple.java").
When You compile the above given class using
javac mainoverloading.java
It is successfully compiled and a class file named simple.class is generated in your folder.
You can then run it by typing
java simple.
But this is actually not a good practice, as Elliott Frisch said rename your class into simple.java.
have a look For Windows- HelloWorld
For Linux - HelloWorld
Java allow us to use any name for file name, only when class is not public.
Its running nicely, because for eclipse main class it simple, its smart to identify that simple.class will be created.
If you are running from command line ,
class file created for your code is simple.class so JVM will be unable to find mainoverlading.class

Error: Could not find or load main class Mainclass

So I followed this site here and I don't think the part where I went wrong was the first part when it was talking about directories and stuff. I'm a real noob. Anyways, when I try to run the java program, it says Error: Could not find or load main class Mainclass. Any idea what's going on? I've tried installing the java jdk but that doesn't make a difference.
Details:
Here's a screenshot of what my page looks like.
Is there something I should change in my .build file? (I used the same one on the site). Thanks in advance.
You need to have a class called Main (or main) with a method like this one:
public static void main(String [] args){
//your main class code here
}
EDIT:
Try renaming your class to "Main" (no quotes).
Large Sized EDIT:
1.You need to make your class public class MainClass
2.You MUST have a package declaration!!!
3.You SHOULD probably rename your class to "Main" and your java file to "Main". Other than that, your programming is pretty solid!
Try below code:
sudo apt-get update
sudo apt-get install default-jdk
I cannot see your code but this is a sample code:
class greeting {
public static void main(String args[]) {
System.out.print("hi");
}
}
Happened to me recently, the Class file saved in your Cloud9 folder has a lower case letter, different from the Class name uppercase
Filename: Mainclass
ClassName: MainClass

"Hello World" program has generated an exception

I am new to Java and its my first program in Java, I am trying to run "Hello World" App program but its giving an error. It has compiled but has generated this error:
Exception in thread "main" java.lang.NoClassDefFoundError:
HelloWorldApp/class Caused by: java.lang.ClassNotFoundException:
HelloWorldApp.class at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
I wrote the given code in Note Pad and saved that in C:\Program Files x86)\Java\jdk1.6.0_14\bin
code:
public class HelloWorldApp{
public static void main(String[] args){
System.out.println("Hello World");
}
}
Remeber that you must use specific filename in java so:
If your class is HelloWorldApp, you must put it into a file named HelloWorldApp.java
Then you must compile with javac HelloWorldApp.java.
Now, you can execute the result .class file with java HelloWorldApp
use only class, not public class.
public class can only be used when your java code file name is same as your public class name in which your main method is present.
use
class HelloWorldApp{
public static void main(String[] args){
System.out.println("Hello World");
}
The runtime's not finding the actual class file, rather than any issue with the code itself. For a starter though, you should be working in your own folder tree and be treating anything under "Program Files" as Read-only and not anywhere to be creating files.
Create your own folder elsewhere, compile then run, possibly specify the class path when running.

Interface Implementation error : cannot find symbol

I am implementing the following sample interface:
package test1;
public interface MotorVehicle {
void run();
int getFuel();
}
In the class
package test1;
import test1.MotorVehicle;
public class Car implements MotorVehicle
{
int fuel;
public void run(){
System.out.println("Running");
}
public int getFuel(){
return this.fuel;
}
}
When I try to compile the class file , I get the following error :
Car.java:4: error: cannot find symbol
public class Car implements MotorVehicle
^
symbol: class MotorVehicle
1 error
Compile Steps:
Step:1 javac MotorVehicle.java
Step:2 javac Car.java
Both my interface and the class are in the same directory , why does ut come up with cannot find symbol error?
Edit:
As suggested , have changed the package , and tried to run the same code again . Still getting an error.
The problem is that you're in the wrong folder when compiling.
From the console screenshot, it is clear that you are inside /test1. However, the package test1; statement expects a folder inside the current folder named test1. It can't find that folder/package, so you get an error.
The solution is to go up one folder, so you end up in /src, then compile using the path to the file, e.g. javac test1/Car.java. Explanation: You are in the folder /src, the package statement inside the classes says they are inside the folder test1 which is inside /src. Now every package/path can be resolved.
And you shouldn't import things that are in the same package.
First of all as your package name is test you must keep your class and the interface in a folder named test.
Second thing since they are in the same folder named test remove import test.MotorVehicle; from the class defination
Suppose if your folder test resides in g:/ such that g:/test/contains class and the interface.
Then try opening the command prompt in g:/
then type the following commands
for compiling
javac test/Car.java
and for executing
java test.Car
Though you may get Error: Main method not found in class test.Car
as your class does not contain main mathod
You are going in to exact path by the use of cd command.Because of that interface is not accessible as class will try to find out it from package from current/running location.
For make this compile you have to specify fully (again Fully) qualified name of package during compilation.
For Example
If you class is in a.b.test package compile it like this
javac a/b/test/Car.java
First compile MotorVehicle as it doesn't have any dependencies. Then set the classpath
Before issuing javac Car.java compile statements you need to set the Classpath
Windows
set CLASSPATH=%CLASSPATH%;<PATH_TO_COMPILED_BINARY>/
Unix
export CLASSPATH=$CLASSPATH:<PATH_TO_COMPILED_BINARY>/
<PATH_TO_COMPILED_BINARY> should not include the package test1
Example :
C:/sourcecode/test1
Then <PATH_TO_COMPILED_BINARY> should be C:/sourcecode
Update
Removing the import test1.MotorVehicle will also fix the issue.
After Compiling Motorvehicle.java. you have to create a folder test1 and transfer the MotorVehicle.class into the folder test1 then compile the next file Car.java. This will solve your error

How to invoke Akka helloworld?

Below is the code taken from http://doc.akka.io/docs/akka/2.2.3/AkkaScala.pdf
import akka.actor.Actor
object Greeter {
case object Greet
case object Done
}
class Greeter extends Actor {
def receive = {
case Greeter.Greet =>
println("Hello World!")
sender ! Greeter.Done
}
}
In it, it says :
How can I run this as a standalone within Eclipse ?
I've tried creating a new Run configuration, setting com.example.HelloWorld as the main class with the program argument being akka.Main but I receive "main class not found" exception.
Update :
Based on answer by TheTerribleSwiftTomato I have
input akka.Main into the Main class field, and
added com.example.HelloWorld as the sole argument in the Arguments tab.
but I receive below error :
Exception in thread "main" java.lang.ClassNotFoundException: com.example.HelloWorld
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at akka.actor.ReflectiveDynamicAccess$$anonfun$getClassFor$1.apply(DynamicAccess.scala:67)
at akka.actor.ReflectiveDynamicAccess$$anonfun$getClassFor$1.apply(DynamicAccess.scala:66)
at scala.util.Try$.apply(Try.scala:161)
at akka.actor.ReflectiveDynamicAccess.getClassFor(DynamicAccess.scala:66)
at akka.Main$.main(Main.scala:32)
at akka.Main.main(Main.scala)
I have added the akka Maven dependency, is there something else I'm missing ?
akka.Main is not an argument, it is the launcher class (i.e. the one that contains the main method). In this case, as described in the documentation, it will set up the ActorSystem instance and other necessary infrastructure.
So, in Eclipse, you would:
input akka.Main into the Main class field, and
add com.example.HelloWorld as the sole argument in the Arguments tab.
Re edit: I see two problems:
in the snippet you posted in the question, you don't actually appear to have the HelloWorld class from the example. Did you remember to include in your project?
even if you did remember to include it, there's a slight error in the HelloWorld class. It's missing the package declaration:
package com.example
Correct that (move it to the right package) and you should be on your way to writing Actor systems.

Categories