Java Class name with _ - NoClassDefFoundError with "wrong name" [duplicate] - java

I have a problem while trying executing my java application.
Whenever I try to execute the program through the command
java ProgAudioJ
I get this error:
Exception in thread "main"
java.lang.NoClassDefFoundError: ProgAudioJ (wrong name: es_2011/ProgAudioJ)
at java.lang.ClassLoader.defineClass1(NativeMethod)
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(NativeMethod)
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: ProgAudioJ. Program will exit.
If I remove from my code:
package es_2011;
Everything works perfectly. How do I solve the problem?

Because I found these answers unclear, here is what you need to do.
First, if you package your code (IE your classes have the package keyword at the top) the compiled classes have to be in a directory with the same name as your package declaration in code. After you have compiled your classes, you need to move up a directory when you exectute the java command, and you include the name of the package. For example, if your code exists in /myFolder/myPackage/ , and your class starts with package myPackage (note that the directory and the package are the same name), then you would do the following (linux / osx):
cd /myFolder/myPackage
javac MyClass.java
cd ..
java myPackage.MyClass
Edit - A late edit to clarify something I see people get confused on. In the example above, the package is only one deep, meaning its just myPackage. If you code has a larger package, like
package com.somedomain.someproject;
you will need to execute the java command from the directory which contains the root directory for that package. For example if your compiled code is in myCode/com/somedomain/someproject/MyMainClass.class, then you will execute the java command from the myCode folder, like this (Again, take special note that the directory structure is the same as the package declaration):
cd /myCode
java com.somedomain.someproject.MyMainClass

Try using:
java es_2011.ProgAudioJ
(instead of java ProgAudioJ).
I'm making some assumptions here about your current working directory and your CLASSPATH. If you can provide information about the command you're running (e.g. what directory you're in, where the class file is located, etc.), we can help you more efficiently.

Try this (compile and run):
dir
2011-02-10 00:30 <DIR> .
2011-02-10 00:30 <DIR> ..
2011-02-10 00:27 58 es_2011
javac es_2011/ProgAudioJ
java es_2011.ProgAudioJ

It's quite clearly stated there:
java.lang.NoClassDefFoundError: ProgAudioJ (wrong name: es_2011/ProgAudioJ)
If you want to put a class in a package(*), then the source code must be placed in a corresponding directory, e.g.,
src/Main.java <- root package (no declaration)
src/es_2011/ProgAudioJ.java <- package es_2011;
(*) You should do it always, except for tiny throw-away stuff and possibly for the main class.

Try this,
Compile your class using below command
$ javac ProgAudioJ.java -d .
Run your application by command
$ java es_2011.ProgAudioJ

The reason that it works when you remove
package es_2011
is that you are changing how the compiler packages up, and effectively locates, the file.
I had the same problem - and the error message wrong name: does indeed point you to the answer. You are using the wrong name "ProgAudioJ" in order to run the .class file.
It has been packaged up as
es_2011/ProgAudioJ
In order to run it - you have to either move up a directory:
If you are here: (Windows)
src\es_2011\
move to
src\
Then run the line:
java es_2011.ProgAudioJ
This tells the compiler to look for the ProgAudioJ - which resides in the es_2011 package. For a standard installation, this will be based on folders - so it will look for the es_2011 folder first, and then the name of the .class file that you want to run (ProgAudio).

Related

cobertura-instrument.sh fails to instrument jar file with java.lang.NoClassDefFoundError: net.sourceforge.cobertura.instrument.InstrumentMain

I'm trying to instrument jar file (from Spacewalk project) so I can measure code coverage of mine testing, but it is failing:
# /opt/cobertura-2.1.1/cobertura-instrument.sh --datafile /tmp/out /usr/share/rhn/lib/rhn.jar
Exception in thread "main" java.lang.NoClassDefFoundError: net.sourceforge.cobertura.instrument.InstrumentMain
Caused by: java.lang.ClassNotFoundException: net.sourceforge.cobertura.instrument.InstrumentMain
at java.net.URLClassLoader.findClass(URLClassLoader.java:432)
at java.lang.ClassLoader.loadClass(ClassLoader.java:676)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:642)
Could not find the main class: net.sourceforge.cobertura.instrument.InstrumentMain. Program will exit.
I have tried to provide one random class (in the ideal state I want to instrument all of them) from that jar as well, but with same result:
# jar tf rhn.jar | tail
org/cobbler/CobblerConnection.class
[...]
# /opt/cobertura-2.1.1/cobertura-instrument.sh --datafile /tmp/out /usr/share/rhn/lib/rhn.jar org.cobbler.CobblerConnection
I'm pretty sure I'm just missing something what it is trying to tell me.
I'm using cobertura-2.1.1 downloaded from SourceForge and extracted into /opt, running on Red Hat Enterprise Linux 6.
OK, this was simple:
# dos2unix /opt/cobertura-2.1.1/cobertura-instrument.sh
also it is missing bash shebang (#!/bin/bash), so you might need to add it to the beginning of the file (I do not know why it worked for me even without that).

Why am I getting a NoClassDefFoundError in java?

I managed to successfully compile my code, but I'm not able to execute it. How do I fix this?
scannerTesting is my package and ScannerTesting.java is my main file.
D:\Java>javac Testing\src\scannerTesting\ScannerTesting.java
D:\Java>java Testing\src\scannerTesting\ScannerTesting
Exception in thread "main" java.lang.NoClassDefFoundError: Testing\src\scannerTesting\ScannerTesting <wrong name: scannerTesting/ScannerTesting>
.
.
.
java -cp ./Testing/src scannerTesting.ScannerTesting
When you run java, it looks for matching classes within its classpath. So what these arguments are doing is add your source folder to the classpath using -cp, and specify that the class that should be run is scannerTesting.ScannerTesting.
For more information, check out java cli tool documentation at Oracle
scannerTesting is your package name,right?
If so, I suggest you run command "java" under the workspace D:\Java\Testing\src
java scannerTesting.ScannerTest

How do I run a Java class in a package?

I have two java classes as follows
App1 without a package:
class App1 {
public static void main(String[] args) {
System.out.println("App1 hello world...");
}
}
App2 in a package:
package java.java.package1;
class App2 {
public static void main(String[] args) {
System.out.println("App2 hello world...");
}
}
I can compile them both:
D:\javaTest>javac App1.java
D:\javaTest>javac App2.java
However, I can only run the first one:
D:\javaTest>java App1
App1 hello world...
D:\javaTest>java java.java.package1.App2
Exception in thread "main" java.lang.NoClassDefFoundError: java/java/package1/App2
Caused by: java.lang.ClassNotFoundException: java.java.package1.App2
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: java.java.package1.App2. Program will exit.
How can I run App2?
If you put the source in an appropriate directory hierarchy matching the package name (D:\javaTest\java\java\package1\App1.java), and compile/run from the root of the hierarchy (D:\javaTest), you wouldn't have this problem:
D:\javaTest>javac java\java\package1\App1.java
D:\javaTest>java java.java.package1.App1
App2 hello world...
You can also compile using the -d option so that the classes are moved into such a directory hierarchy:
javac -d . App2.java
java java.java.package1.App2
Note you shouldn't use a package name starting with java, and later versions of the JDK will throw a SecurityException. See this question for more information.
You create a new directory. This is the directory containing your work, and is not the start of your packages.
For instance, I create folder /terri to start.
I then create the folder structure /clarie/andrea under it.
My package is going to be called claire.andrea in this example.
Normal package names start with com and then a company name or something like that (or java for standard java packages, so don't use that: like java.lang.*).
In the andrea folder, I create a java file called Saluton.java with the class Saluton (which just print hello). The class name and the filename must match.
To compile, from the terri/ folder: javac .\claire\andrea\Saluton.java
This will create a Saluton.class in the \terri\claire\andrea\Saluton.class
To run: (again from /terri), I do: java -cp . claire.andrea.Saluton
Which says, use class path from my current directory.
My main program is in the package claire.andrea and the Class name is Saluton.
Here's the run:
\terri
java -cp . claire.andrea.Saluton
"Hello World".
To sum it up, the package name much match the underlying directory structure.
The file (if it references a package) must live inside the directory stucture it is refering. If I compile Saluton.java in /terri with package claire.andrea I have not found a way to run it, it does compile fine.
Also, the filename for the class must match the public class in that file.
To run, package.Class. In general, packages are not capitalized and Classes are.
You have to put -d directory Options..because
Set the destination directory for class files. If a class is part of a
package,
javac puts the class file in a subdirectory reflecting the package name,
creating directories as needed. For example, if you specify -d c:\myclasses
and the class is called com.mypackage.MyClass, then the class file is called
c:\myclasses\com\mypackage\MyClass.class.
App2 needs to be in the java/java/package1 directory. In java, the package name and the directory for the source code must match. Did you not get a compilation error?
Check what is your classpath value by below command in command prompt
echo %CLASSPATH%
check where your class is getting created.
to compile a java source program you need to check the path you are giving , whether java file is available there or not.

Run Eclipse project from command line

I am having two problems regarding compiling and running an Eclipse java project from command line. This works fine when I am just running from the eclipse IDE. I tried googling but couldn't really get the thing working. Any help is much appreciated.
Problem 1: When I try to compile from a location different from the directory where the .java file is, it throws the error "cannot read: myfile.java". But if I migrate to this directory then it compiles.
The command that I was giving is (when in some other directory):
javac -cp C:\ABC\src\XYZ myfile.java
The command that I was giving when in XYZ directory:
javac myfile.java
This generated two .class files myfile.class and Testing_Thread.class(I guess this because I have a thread in my code)
Problem 2: After I have compiled by going to its directory, when I try to run the program, I get the error "Exception in thread "main" java.lang.NoClassDefFoundError: myfile (wrong name: XYZ/myfile.java)" even when I am trying to run from the XYZ directory. I get the same error when I try to run from some other place also.
The command that I was giving when in XYZ directory:
java myfile
The command that I was giving when in some other place:
java -cp C:\ABC\src\XYZ myfile
I am also attaching a hierarchy of my directory structure if it is of any help:
These examples assume the following source structure:
C:\temp\compile-test\src\a\b\c\D.java
Where D.java is:
package a.b.c;
public class D { }
The first problem, cannot read: myfile.java, is because it is not correct to use the cp command line option to point to your source code.
C:\temp\compile-test\src>javac -cp c:\temp\compile-test\src\a\b\c D.java
javac: file not found: D.java
Usage: javac <options> <source files>
use -help for a list of possible options
This should instead be the following, where javac is run from your source folder, and we can use relative paths to the source files (NOTE - javac is run from the source folder here):
C:\temp\compile-test\src>javac a\b\c\D.java
Or this, where we specify full paths to the source files, and javac can be run from anywhere (NOTE - javac is run from C:\ here):
C:\>javac temp\compile-test\src\a\b\c\D.java
Both of the above options will result in your class files being created in the same folder as the source. I.e.:
C:\temp\compile-test\src\a\b\c\D.class
For the second problem, if you try and run a class that has a package name from 'inside' the package, this will result in the name being wrong (NOTE - java being run from 'inside' the package here):
C:\temp\compile-test\src\a\b\c>java D
Exception in thread "main" java.lang.NoClassDefFoundError: D (wrong name: a/b/c/D)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: D. Program will exit.
To run the D class, you should be at the package 'root', and supply the Fully Qualified Class Name. I.e.:
C:\temp\compile-test\src>java a.b.c.D
Exception in thread "main" java.lang.NoSuchMethodError: main
Note I get an exception as the D class doesn't have a main method, and so cannot be run. To fix, we add a main method:
package a.b.c;
public class D {
public static void main(String[] args) {
System.out.println("main");
}
}
and re-run:
C:\temp\compile-test\src>java a.b.c.D
main

Protocol Buffers Invalid embedded descriptor problem

I'm having some problems at runtime with some of my generated protocol buffer classes.
My project layout is as follows:
module/
protobuf-api/
proto/
com/foo/api/Service.proto
com/foo/shared/Shared.proto
org/bar/api/Message1.proto
org/bar/api/Message2.proto
The Service.proto file depends on Shared.proto and some of the Message*.proto files. From the protobuf-api directory, I run the following command to compile:
find . -name *.proto -exec protoc --java_out=java -I=proto {} \;
When I attempt to run my Service, I get the following exception:
java.lang.ExceptionInInitializerError
at com.linkedin.history.api.protobuf.HistoryServiceProtos$HistoryServiceQuery.(HistoryServiceProtos.java:544)
at com.linkedin.history.api.serializer.HistoryServiceSerializer.serialize(HistoryServiceSerializer.java:47)
at test.history.serializer.TestSerializer.testHistoryServiceQuery(TestSerializer.java:38)
at test.fwk.util.core.BaseTestSuiteCore.run(BaseTestSuiteCore.java:304)
at test.fwk.util.core.BaseTestSuiteConf.run(BaseTestSuiteConf.java:186)
at test.fwk.lispring.BaseTestSuite.run(BaseTestSuite.java:232)
at test.fwk.lispring.BaseTestSuite.callAppropriateRun(BaseTestSuite.java:265)
at test.fwk.util.core.BaseTestSuiteCore.run(BaseTestSuiteCore.java:199)
Caused by: java.lang.IllegalArgumentException: Invalid embedded descriptor for "com/linkedin/history/api/protobuf/HistoryService.proto".
at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(Descriptors.java:268)
at com.linkedin.history.api.protobuf.HistoryServiceProtos.(HistoryServiceProtos.java:1794)
Caused by: com.google.protobuf.Descriptors$DescriptorValidationException: com/linkedin/history/api/protobuf/HistoryService.proto: Dependencies passed to FileDescriptor.buildFrom() don't match those listed in the FileDescriptorProto.
at com.google.protobuf.Descriptors$FileDescriptor.buildFrom(Descriptors.java:221)
at com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(Descriptors.java:266)
I've read the post here but I think I'm doing everything correctly. Any suggestions on why I'm having the initializer errors? I'm compiling everything with the same -I flag.
I suspect that the problem is that when you're finding the proto file, you've given it the full path, e.g. proto/com/foo/api/Service.proto but when it refers to it via the include directory, it's using com/foo/api/Service.proto
Simple fix - run this from the proto directory:
find . -name *.proto -exec protoc --java_out=../java -I=. {} \;
I must admit I can't remember a lot of the details of protoc (which I really should) but I suspect that will work.
Another alternative which may work:
protoc --java_out=java `find . -name '*.proto'`
i.e. pass all the proto files into a single call to protoc.
I had the same error type in C# and here was my problem: I called the protoc in a pre-build step in my project. There I used Visual Studio built-in macros like $(SolutionDir) and $(ProjectDir) to retrieve necessary paths. Since I referenced *.proto files from other projects, I used two --proto_path options: one for the root path (to resolve import paths) and one for the file itself. My solution file was inside a subdirectory of the root directory, so I used the relative path .. to get to the root. Proto files are always in subdirectory gen of the particular project. All in all, the command was like this:
protoc.exe --proto_path=$(SolutionDir).. --proto_path=$(ProjectDir)gen $(ProjectDir)gen\DemoFile.proto
It compiled fine, but I got the System.TypeInitializationException at runtime on calling CreateBuilder() method. The problem was that both paths $(SolutionDir).. and $(ProjectDir) (though effectively pointing to the same directory) had different textual representation due to the relative path component ... I solved the problem by consistently using the same path like this:
protoc.exe --proto_path=$(SolutionDir).. $(SolutionDir)..\My\Demo\Project\Directory\gen\DemoFile.proto
It cost me almost 3 days to narrow down and recognize the problem, so I share my solution here in hope that it will save some time for someone.

Categories