I'm going through a Java course, using Intellij IDEA. In this "Test" project, I have only two test classes as shown below (they really don't have any code).
Before I get into the problem, I just need to state that all my projects run correctly, it's just that I have the following problem with running the terminal command, and I'm afraid I messed something up with my IDE:
The problem occurs when I need to get the serial version of the class BankAccount using the following command:
E:\Software Projects\Test\out\production\Test>serialver com.company.BankAccount
I get the error that is shown below, and I understand that the classes were compiled by a newer Java Runtime error, but how do I troubleshoot this in Intellij? I get the same error when running the command on any class in any project.
Additional note: I also get an error related to this one when running the Main class in terminal using java com.company.Main
What I think could be the cause of the problem: Prior to even trying "serialver" for the first time, I changed the name of a project folder (I just used "Rename" in my file explorer in Windows, and it messed everything up in Intellij (whatever project I opened, it had no SDK and no Run/Debug Configurations). So, I selected "Add SDK" on a project (or something similar, and selected a JRE 12 from the list.
Then I ran "Debug Main" and the Run/Debug Configurations were set automatically.
When I go to Project Structure > Project Settings > SDKs I only have one SDK :"C:\Program Files\Java\jdk-12.0.1".
When I go to "Run/Debug Configurations" in a project, in the JRE of Application > Main I have the following to select (but as far as I understand, these are all one and the same SDK, right?):
Default (12 - SDK of 'Test' module)
12(java version "12.0.1")
"C:\Program Files\Java\jdk-12.0.1"
So I don't know how I can adjust anything here? How was I able to compile these classes at a newer Java Runtime version at any point?
The problem here is that I'm not sure if something else is wrong, or if the prior adjustments I've made have messed something up.
The classes:
package com.company;
public class Main {
public static void main(String[] args) {
// write your code here
}
}
package com.company;
public class BankAccount {
}
When running
E:\Software Projects\Test\out\production\Test>serialver com.company.BankAccount
What I get in the terminal is the following error:
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/company/BankAccount has been compiled by a more recent version of the Java Runtime (class file version 56.
0), this version of the Java Runtime only recognizes class file versions up to 55.0
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/java.net.URLClassLoader.defineClass(URLClassLoader.java:550)
at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:458)
at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:452)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:451)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:398)
at jdk.compiler/sun.tools.serialver.SerialVer.resolveClass(SerialVer.java:108)
at jdk.compiler/sun.tools.serialver.SerialVer.serialSyntax(SerialVer.java:80)
at jdk.compiler/sun.tools.serialver.SerialVer.main(SerialVer.java:188)
Error when running the Main class with E:\Software Projects\Test\out\production\Test>java com.company.Main:
Error: LinkageError occurred while loading main class com.company.Main
java.lang.UnsupportedClassVersionError: com/company/Main has been compiled by a more recent version of the Java Runtime (class file version 56.0), this version of the Java Runtime only recognizes class file versions up to 55.0
Run java -version in the command line. If it's not Java 12 or later, it will explain the issue. You are targeting Java 12 version while run it on an older Java version.
The solution would be to either adjust the target language level so that your code can run on older Java versions or to adjust your PATH environment so that java from JDK 12 install directory is the first. Or you can specify the full path to java.exe in the command line when trying to run the code:
E:\Software Projects\Test\out\production\Test>"C:\Program Files\Java\jdk-12.0.1\bin\java.exe" com.company.Main
Solved by editing file "idea.properties" and setting proper JVMVersion
Related
I'm using jpackage (part of jdk14+) to build an MSI for windows installation of my java application. Everything packages and installs fine, but when I go to run the .exe it throws an exception while trying to initialize Sentry.
Exception in thread "main" java.lang.NoSuchMethodError: 'void jdk.internal.reflect.Reflection.registerMethodsToFilter(java.lang.Class, java.lang.String[])'
at sun.misc.Unsafe.<clinit>(Unsafe.java:59)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Unknown Source)
at com.google.gson.internal.reflect.UnsafeReflectionAccessor.getUnsafeInstance(UnsafeReflectionAccessor.java:70)
at com.google.gson.internal.reflect.UnsafeReflectionAccessor.<init>(UnsafeReflectionAccessor.java:34)
at com.google.gson.internal.reflect.ReflectionAccessor.<clinit>(ReflectionAccessor.java:36)
at com.google.gson.internal.ConstructorConstructor.<init>(ConstructorConstructor.java:51)
at com.google.gson.Gson.<init>(Gson.java:206)
at com.google.gson.GsonBuilder.create(GsonBuilder.java:597)
at io.sentry.EnvelopeReader.<init>(EnvelopeReader.java:28)
at io.sentry.SentryOptions.<init>(SentryOptions.java:89)
at io.sentry.SentryOptions.empty(SentryOptions.java:1559)
at io.sentry.NoOpHub.<init>(NoOpHub.java:15)
at io.sentry.NoOpHub.<clinit>(NoOpHub.java:13)
at io.sentry.Sentry.<clinit>(Sentry.java:24)
This is using the latest sentry 5.2.0 library (tested all the way back to 4.2 and it still has the error though), and have tested JDK 14 through 17 to see if any of them work, which they don't.
Everything works fine if I'm running the .jar with java -jar program.jar, but it seems like jpackage creates an executable that doesn't work quite the same as simply running java.
The call to jpackage needed --add-modules jdk.internal.vm.compiler to fix this in JDK14, and by JDK17 it needs to be --add-modules jdk.unsupported instead.
Following this post, I am using the following steps to compile the parser/lexer from this repository:
export CLASSPATH=".:/usr/local/Cellar/antlr/<version>/antlr-<version>-complete.jar:$CLASSPATH"
antlr <grammarName>.g4 -o <someFolder>/
javac <someFolder>/<grammarName>*.java
but when I use the instructions here:
grun <someFolder>/<grammarName> tokens -tokens < <inputFile>
I get this error messages:
Exception in thread "main" java.lang.NoClassDefFoundError: IllegalName: <someFolder>/<grammarName>Lexer
at java.base/java.lang.ClassLoader.preDefineClass(ClassLoader.java:889)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1014)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:825)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:723)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:646)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:604)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at org.antlr.v4.gui.TestRig.process(TestRig.java:129)
at org.antlr.v4.gui.TestRig.main(TestRig.java:119)
I would appreciate if you could help me know what is the problem and how I can resolve it.
I don’t see where you’ve specified a package name, so now your Java classes are located in <someFolder>. Be sure to compile them in that folder.
Then you’ll need to add that folder to your classpath (probably instead of “.”)
Try adding <someFolder> into the CLASSPATH you’re exporting. Then leave it off of your grun command line.
Java will only load classes from the Classpath (it’s a security thing). When TestRig runs, it attempts to load your class by building the Java class name it would have produced for you Parser (and Java will have to find that class somewhere in the classpath).
Your could modify the grun alias to allow for you to specify a directory to search for your classes, and use the -cp option on the Java command, but that’s probably more trouble than just adding it to you classpath that you’re using for this testing.
I stumbled upon a weird error while using JDBC sqlite with org.sqlite.JDBC
my code compiles and runs fine on Windows.
But when I tried moving it to Ubuntu it started showing this:
Exception in thread "main" java.lang.ClassNotFoundException: org.sqlite.JDBC
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at mall.SQLiteJDBC.<init>(SQLiteJDBC.java:27)
at mall.AllegroReader.<init>(AllegroReader.java:33)
at mall.Mall.main(Mall.java:31)
I'm running it with java -classpath "sqlite-jdbc-3.7.2.jar" -jar Mall.jar" and java -classpath "sqlite-jdbc4-3.8.2-SNAPSHOT.jar" -jar Mall.jar
with both versions in the same directory as my jar and I've tried a dozen different options specifying classpath and it behaves exactly the same. I tried openjdk and oracle jdk.
I tried rebuilding it on Ubuntu, changing ant .xmls, changing paths, etc.
I have no idea what is going on. Pls help.
Here is what happens inside my dist directory:
work1#workwork:/var/www/mall/dist$ ls
mall.db Mall.jar Mall.jar.old sqlite-jdbc-3.8.4.3-SNAPSHOT.jar
work1#workwork:/var/www/mall/dist$ java -classpath "sqlite-jdbc-3.8.4.3-SNAPSHOT.jar:Mall.jar" Mall
Error: Could not find or load main class Mall
The classpath is ignored when you use -jar.
You have to either include the dependencies in the jar (or at least have the jar manifest point to them), or run it with -classpath sqlite.jar:Mall.jar the.main.class.
Error: Could not find or load main class Mall.main. all files are there,
my main class comes from Mall.java and is in mall package which
compiles to Mall.jar
So the correct command line is:
java -classpath "sqlite-jdbc-3.8.4.3-SNAPSHOT.jar:Mall.jar" mall.Mall
OP findings
to view the classes in jar use jar tf Mall.jar - from this I got mall/Mall.class meaning my class containing main was mall.Mall
it showed
mall/Mall.class
so I should have used mall.Mall as the class to run (instead of pulling my hair)
After spending over 6 hours total with many failed attempts at running "portable" jar package using classpath and whatnot, after having tried OneJar and jarjar to no avail (ended up with Class file too large!) I decided to write the offending piece of code in PHP.
It proved to be more portable than Java in my case.
I have configured JDEE in emacs. But when I try to run any App, it throws NoClassDefFoundError .
Upon compilation, although a class file is created, it only shows the buffer for sometime and then closes it.
The same files if compiled and run from the terminal work perfectly fine.
Also, on this machine I have java-6-openjdk installed. I had configured JDEE on my home computer which has sun java jdk and there are no such problems there.
Other than this, features such as code completion, generation etc work fine.
How do I solve this? Is this due to open jdk ?
I am using a Ubuntu 10.04 desktop.
This is the stack trace
cd /home/vineet/java/KodeJava/src/org/kodeplay/kodejava/
/usr/lib/jvm/java-6-openjdk/bin/java org.kodeplay.kodejava.EmacsTest
Exception in thread "main" java.lang.NoClassDefFoundError: org/kodeplay/kodejava/EmacsTest
Caused by: java.lang.ClassNotFoundException: org.kodeplay.kodejava.EmacsTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: org.kodeplay.kodejava.EmacsTest. Program will exit.
Process org.kodeplay.kodejava.EmacsTest exited abnormally with code 1
Thanks
something in your config is wrong. you are trying to run from the "/home/vineet/java/KodeJava/src/org/kodeplay/kodejava/" directory, but in order to find your class on the classpath, you should be running from "/home/vineet/java/KodeJava/src/". Probably need to configure the "jde-run-working-directory" variable.
From the question, it sounds as if the JDEE configuration is not complete when it comes to setting up the command to be launched when running the Java App.
If the same Java App can be run from the terminal with no issues, OpenJDK is not causing the trouble, I'd wager.
Perhaps you copied over from the other computer the JDEE configurations and did not adopt them for the new computer that has OpenJDK.
I created a new project and a new class in eclipse in my ubuntu machine to write the simple Hello World program. It works fine as long as i write the main method and use System.out.println to print my "Hello World". but i want to use the acm.jar package so I imported it to my project and tried to extend the ConsoleProgram class in acm.jar but once i write the public void run() method and try to run it i get some error
i know that i have imported acm.jar successfully because eclipse suggested the rest of the import when i was typing import acm.program.*;
here is the results i get:
Exception in thread "main" java.lang.NoClassDefFoundError: sun.applet.AppletViewer
at gnu.java.lang.MainThread.run(libgcj.so.90)
Caused by: java.lang.ClassNotFoundException: sun.applet.AppletViewer not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:/home/bilm3z/workspace/xbSampleProject1/bin/,file:/home/bilm3z/workspace/acm.jar], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
at java.net.URLClassLoader.findClass(libgcj.so.90)
at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.90)
at java.lang.ClassLoader.loadClass(libgcj.so.90)
at java.lang.ClassLoader.loadClass(libgcj.so.90)
at gnu.java.lang.MainThread.run(libgcj.so.90)
here is a snapshot of the situation:
http://www.mypicx.com/12302009/situation1/
It follows from your stack trace that you're using a GCJ JVM. Try installing the Sun JVM - there are instructions here. (Note that you might have to enable different repositories for this, and note the update-java-alternatives command.)
After you install the Sun JVM, update your Eclipse preferences according to Bozho's answer, so that your Eclipse uses the correct JVM.
That's strange. Try Right click on your project > Build path > Configure built path > libraries, remove the JRE / JDK from there (if it exists), and add it anew - Add library > JRE System Library