I've seen questions like this on here, but I can't find one that is specific to my situation, so sorry if this is a worn out question.
I've got a class P that is part of a package myname.utils located in ~/JavaClasses/myname/utils. I've got another class Printing that is not declared as a part of any package but is located in ~/JavaClasses/myname/practice. Class Printing imports class P. Both of these classes compile fine from ~/JavaClasses directory. However when I attempt to run the compiled class Printing like this
~/JavaClasses$ java myname/practice/Printing
I get the following errors:
Exception in thread "main" java.lang.NoClassDefFoundError: myname/practice/Printing (wrong name: Printing)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
The error is not related to myname.utils.P class but it happens because Printing class has no package definition. This means that the real full name of the class is Printing, but if you launch the java command from ~/JavaClasses folder, you are exactly stating that the full name of Printing class is myname.practice.Printing.
You have 2 options to fix this problem:
1) Declare the myname.practice package inside Printing class (I suggest this one)
2) Add the myname.practice folder to the classpath, in this way:
~/JavaClasses$ java -cp myname.practice:. Printing
The . represents the current directory, and it is necessary so you can still reference the myname.utils.P class from ~/JavaClasses folder.
Side note. At compile time you have no errors because probably you don't specify a classpath when you launch javac and so the current folder it is used. This way, myname.utils.P can be referenced without problem by every class, even by Printing.
This is because you are not using the package name. Yes when java gives the error and the package name for class not found, it uses /. However those should be replaced with .. since parts of the package name are always separated with a period and never with a slash. So if I had a package foo.bee. Then when I tried to run
java foo.bee
It would run currently and all, however if I tried
java foo/bee
it would complain about a class not def error.
Related
I'm trying to use Sublime Text 2 on my Mac in order to write my code for a class I'm taking. I want to test to make sure I can do this first with a basic program, however I'm coming up with an error when I try to run it. I have the build system set to JavaC, and the code is compiling fine, it's just when I try to run it I get issues. Here are my steps:
$ cd Documents/school/algorithms/assignments/
$ javac Hello.java
$ java Hello
Exception in thread "main" java.lang.ClassFormatError: Truncated class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:637)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
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)
I've tried to research what this means but I'm coming up short. Here is my code for Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
If anyone can help me with this issue it would be greatly appreciated. Let me know if you need anymore information.
Your "Hello.java" file doesn't have any issue at all as I can see.
But the error you are getting java.lang.ClassFormatError: Truncated class file is due to an issue with your class file(byte code).
Actually, there is two type of ClassFormatError:
The first one is "Incompatible magic value" which occurs only when you modify the byte code (add something or delete something) in the .class file
The second one "Truncated class file" which you are getting and the reason behind this error is that the .class file doesn't contain any byte code.
So I will suggest you to check your .class file to make sure that it contains some code. If it doesn't, save your code to another directory and then compile and run your code.
I've just fixed the same issue in Android Studio 4.1.1. The error was:
> Task :my_module:kaptGenerateStubsDebugKotlin FAILED
e: java.lang.ClassFormatError: Truncated class file
Restarting Android Studio, invalidating caches were of no help. The only thing that helped me was upgrading Kotlin version (I had 1.4.10) to match the Kotlin Plugin one (I have 1.4.20).
Project-level build.gradle:
buildscript {
// I've upgraded Kotlin version to match the Kotlin plugin one.
ext.kotlin_version = '1.4.20'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Im running a debian java server that needs to send and receive objects of type EventObject and PostObject (e.g serializable). These have been placed in a .jar file SharedModels.jar and are used both in client and server.
On the windows installation (Eclipse), using
import Models.EventObject;
import Models.PostObject;
works fine (including external Jar through Eclipse).
It compiles fine, but when the method is called on the server , the server doesn't recognize the class anymore. This is the output:
Exception in thread "Thread-0" java.lang.NoClassDefFoundError: Models/PostObject
at server.Database.getPosts(Database.java:101)
at server.ServerThread.run(ServerThread.java:47)
Caused by: java.lang.ClassNotFoundException: Models.PostObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
I've understood this is because JVM does recognize the class at compiletime but not during runtime.. OR something is wrong with classpath. Does the name of the actual .jar need to have any naming to fid the package contained within? What do I need to do to fix this?
If your Models/PostObject is a class from some jar, then make sure that the class is included in exported jar/war (simply open it with tool like 7zip and manually confirm that the needed class is there).
How to create a jar with external libraries included in Eclipse?
This is thrown when at compile time the required classes are present , but at run time the classes are changed or removed or class's static initializes threw exceptions. It means the class which is getting loaded is present in classpath , but one of the classes which are required by this class , are either removed or failed to load by compiler .So you should see the classes which are dependent on this class
Followed install instructions at https://github.com/sarmbruster/neo4j-uuid
Seemed to build successfully
Now running into this error, any thoughts?
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/uuid/Generators
at org.neo4j.extension.uuid.UUIDTransactionEventHandler.<init>(UUIDTransactionEventHandler.java:30)
at org.neo4j.extension.uuid.UUIDLifeCycle.start(UUIDLifeCycle.java:29)
at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:498)
... 13 more
Caused by: java.lang.ClassNotFoundException: com.fasterxml.uuid.Generators
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 16 more
You need to download the jar file for the fasterxml.com uuid generator and copy it to Neo4j's plugins folder.
I'll gonna amend the docs for neo4j-uuid with that one.
I think the general answer is that the class in question cannot be found (yeah, that's what it says, right?) Well, the class can't be found because a dependency is missing. If there is no error at compile time, the class could be found then, so that means that there is a difference between your dependency context at compile time and run time. Possibly you are compiling your code against one version of a library, that includes the class, but you run your compiled code against a different version, which doesn't; or else you have a dependency on a jar that was compiled against some dependency that is not provided together with it. As for the specifics of your case, I don't know, I've never used the Neo4j extension in question, but I would look for whatever jar contains the correct version of com.fasterxml.uuid.Generators and make sure its on you classpath.
I hava made a runnable jar file out of six classes:
Main: Contains the main method, and specified in the manifest (I included a new line)
Main$1 and Main$2: 2 anonymous inner classes that are in the main class. (Main$2 is in the main method, but I don't think that really matters.)
Form
Form$1: An anonymous inner class in Form
WrapLayout
I specify these inner classes when making the jar file, but when I look inside it (I am on mac) the inner classes are not in the jar! So when I run it, I get this:
Exception in thread "main" java.lang.NoClassDefFoundError: Main$2
at Main.main(Main.java:64)
Caused by: java.lang.ClassNotFoundException: Main$2
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)
... 1 more
I can't figure out what's wrong. Can somebody please help?
EDIT: I figured it out! Turns out, you need an escape character (\) in front of the dollar signs for the command to recognize them.
You already found your specific answer, but here's a more general one.
As your modify your program, the set of classes with automatically generated names (e.g., Main$2) will change. Also, if you move your classes into a named package, your jar file will have to have a parallel directory structure. You don't want to have to update your makefile or build script every time this happens. Instead, you should use javac -d to specify a destination directory for the compiled class files, and then jar up this entire hierarchy.
Whilst creating or updating the jar, you can wrap inner class/ anonymous class names in single quotes to avoid the shell interpretting the $ in their names.
I typed in "java -jar ShowTime.jar", and got this error message:
Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value 1347093252 in class file ShowTime
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)
How should I fix this?
P.S. I have a mac.
A Java class should start with the magic (hex) value 0xCAFEBABE (neat huh). Your value 1347093252 is 0x504B0304 in hex, which happens to be the magic value for a ZIP file (first 2 bytes in ASCII are PK for Phil Katz, creator of the ZIP format). A jar is also a zipfile btw, so probably your jar is pretty much corrupt. Try rebuilding the entire project.
That usually means that you compiled the jar for a newer version of java than you ran it with. Check to see if you are using the same version of java to compile and run. If that doesn't fix the problem, please provide more info such as the compiler command and the output of java -version.
It means your application is using a jar file which is complied in a specific format in some other system. Now when you are trying to use the project in other system, the other system is not able to decode the jar, as each system generated a specific key to bundle the project.
Remote debug id the best way to find the issue that which jar is not in the proper format. Run the command in comment prompt for remote debug
java -agentlib:jdwp=transport=dt_socket,server=y,address=8001,suspend=n (command and parameter to run you jar)
what you need is, find the jar file and replace with another jar (may be a freshly downloaded one or if you teammate has the jar. Use it.)
The error i was getting because my project way having dependency with another project ABC, and ABC was using a generic jar. But project ABC was bundled with the magic key. I have replaced the jar used by ABC with a downloaded jar.
I got this error when the main class name of my jar was not identical to its filename.
For example, the main class of the jar was named Foo.java, and inside it I had public class Bar { ... } instead of public class Foo { ... }
It is probably not the most general answer, but making the file name and the class name identical solved this issue for me.