Problem
When i run my HelloWorld, it returns:
"java.lang.UnsupportedClassVersionError: HelloWorld has been compiled
by a more recent version of the Java Runtime (class file version
52.65535), this version of the Java Runtime only recognizes class file versions up to 52.0 "
How do I solve this problem?
Screenshot
Code
public class HelloWorld{
public static void main(String[] args) {
System.out.println("123");
}
}
The issue is connected with Java Debugger extension. I had the same problem and it has been logged in issue log: https://github.com/Microsoft/vscode-java-debug/issues/555
Problem is only with single file applications like HelloWorld. It is ok if you run maven project.
Medsonk's instruction worked for me: https://github.com/Microsoft/vscode-java-debug/issues/555#issuecomment-478464496
summary:
1. make sure uninstall jdk8 clean
2. install jdk11
3. add "vmArgs": "--enable-preview" in launch.json
4. F1, "Java: Clean ……" and "Java: Force ……"
5. run standalone file again
My situation is version conflict between java and javac on Ubuntu;
just run this code:
sudo update-alternatives --config javac
Related
My program is very simple, in a file called app.kt:
import java.net.http.HttpRequest
import java.io.File
fun main(){
println("Hello, world!")
// imported and used File API to show that importing java APIs generally seems to work
println(File("./app.kt").readText())
// the below is necessary to ensure the above "import" isn't removed by the compiler
var build = HttpRequest.newBuilder()
}
At runtime, I get the error java.lang.NoClassDefFoundError: java/net/http/HttpRequest on line 9, after compiling and running the code using the Kotlin command-line compiler.
The above runs without error when compiled and run via IntelliJ on Windows. I'm using WSL2, but have made sure that the versions of java/kotlin I'm using for compilation/runtime on WSL are installed under WSL, not Windows. If somebody could try on a real Ubuntu environment, I would be grateful.
java --full-version prints openjdk 11+28, and the best information I can find says that what I'm trying to import was added in JDK 11, so I shouldn't be having issues.
kotlin -version prints Kotlin version 1.3.72-release-468 (JRE 11+28)
I am compiling with the command kotlinc *.kt -d app.jar -jvm-target 11 and running with the command kotlin -classpath ./app.jar AppKt
EDIT: Note, I do not have the same issue when compiling/running the following Java code:
import java.net.http.HttpRequest;
public class Main {
public static void main(String[] args) {
System.out.println("This will be efef");
HttpRequest.Builder build = HttpRequest.newBuilder();
}
}
I am trying to run a java program in current directory
I executed the ff. commands in current directory where the class file is HelloJNI.class, but it just doesn't work
java HelloJNI
and
java -cp . HelloJNI
Both commands doesn't work.
I also tried setting the CLASSPATH variable and it still doesn't work. Searched a lot but still have no idea.
The error says could not find class HelloJNI. I really can't understand why
------------- EDIT -------------------
package com.greetings;
public class HelloJNI
{
public static void main(String[] args)
{
System.out.println("Whatever");
}
}
adding the package name also results in same error:
Did you compile the Java code? For example:
javac
javac HelloJNI.java
Then you can run (if there are no compile errors).
java HelloJNI
I suggest using Maven or Gradle to build, test, lint etc your Java code. It will help greatly with dependency management and with a reproducible build on other operating systems and on CI/CD systems such as Jenkins and GitLab.
I am new to Java and I want to run a simple test-program on an Ubuntu server via the command line. "Hello World" worked well.
But now I want to add jar-files to my test program. So I followed this simple tutorial.
I created a file "MyTest.java" like this:
import org.apache.commons.lang3.*;
public class MyTest
{
public static void main (String[] args)
{
// Print Hello World!
String x = "Hello World!";
System.out.println(StringUtils.capitalize(x));
}
}
Compiling the java-file with
javac -cp jars/commons-lang3-3.7.jar MyTest.java
worked well without errors and a MyTest.class() file is generated. But when I want to run the test-program with
java -cp jars/commons-lang3-3.7.jar MyTest
I end up with the following Error:
Error: Main class MyTest could not be found or loaded
What am I missing? Why is the compiler successful but yet the program unable to be executed?
EDIT:
To make it clear: I do NOT want to build a jar file (at least not yet). I just want to build and run a simple java program referencing a jar-file i downloaded from the internet. Like described here: https://www.programcreek.com/2014/01/compile-and-run-java-in-command-line-with-external-jars/
Java Version:
> java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)
JAVAC Version:
> javac -version
javac 1.8.0_151
You forgot to specify "MainClass" to the Manifest.mf
That should be in the last step of Eclipse Jar Export Wizard.
Tell me if you're on another IDE or using Maven, that changes the procedure.
Your current command should run successfully.
The solution was to include the current directory.
java -cp jars/commons-lang3-3.7.jar:. MyTest
Thought that would happen automatically. But the dot at the end of the -cp argument was necessary.
I just installed Ubuntu on my pc and I have trouble running my code. I installed eclipse and the latest java jdk. I can compile the with the following command:
javac -cp .:Downloads/jsoup1.8.2.jar workspace/Währungsrechner/src/Crawl.java
but when I try to run it by using the command:
java -cp .:Downloads/jsoup1.8.2.jar workspace/Währungsrechner/src/Crawl
it says that the Class cannot be found and yes I have a method called public static void main(String args[]).
You need to specifiy the class to run when executing the command.
Use java -cp . Downloads/jsoup1.8.2.jar workspace/Währungsrechner/src/Crawl.
By the way, specifying "-cp ." is unnecessary when running a class, so you can just use java Downloads/jsoup1.8.2.jar workspace/Währungsrechner/src/Crawl
A little know feature of the Eclipse's Java compiler is that you can run it from the command line.
This works well (after patching plexus-compiler to use the latest release).
My problem: The stack traces are different when I compile the code from the command line. For example, when I run the compiler in the IDE, I get this output:
at com.some.Foo.method(Foo.java:312)
but when I compile the code from the command line, I get this:
at com.some.Foo.method(com.some.Foo:312)
^^^^^^^^^^^^
What's going here???
Analyzing the class files with javap gives:
SourceFile: "Foo.java"
and
SourceFile: "com.some.Foo"
Any ideas what might cause this?
$ cat baz/Bar.java
class Foo {
}
$ java -jar eclipse/plugins/org.eclipse.jdt.core_3.8.0.v_C19.jar baz/Bar.java
$ javap -c baz/Foo.class
Compiled from "Bar.java"
...
That's Funny, It Works On My Machine?
Win7, Java 7, Eclipse 3.8.0 (identifies itself as Indigo, 20110615-0604).
This seems to have been fixed in plexus-compiler-eclipse version 1.9.1 (or maybe some other version between 1.9.1 and 1.7)
I'm using a patched plexus-compiler-eclipse (I patched the POM to include the 3.7/Helios compiler).
In the class EclipseJavaCompiler.CompilationUnit, there is this code:
public char[] getFileName()
{
return className.toCharArray();
}
which should be
public char[] getFileName()
{
return sourceFile.toCharArray();
}