Why does Java not compile my code? [duplicate] - java

This question already has answers here:
Java: Unresolved compilation problem
(10 answers)
Closed 4 years ago.
Language: Java
JRE: jre1.8.0_144
IDE: Eclipse Oxygen Release (4.7.0)
Description: After I hit the "run" botton, the code did not compile and red text appear in the console.
Screenshot:
Here is the code:
package chapter3.project2;
import java.util.*;
public class MetricConversion {
public static void main(String[] args) {
System.out.println("WTF Eclipse?");
}
}
error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at chapter3.project2.MetricConversion.main(MetricConversion.java:30)
It seems that no matter what I put in the main method, the error just kept popping up.
Thank you.

Where is jre library? Why not compile and run from source folder?
Check project property and add jre runtime library also create source folder (src) and compile and run.
Or
Build a clean Java project and add packages and classes in src folder.

Related

JavaFX application does not compile well when using LauncherImpl instead Application [duplicate]

This question already has answers here:
Java 9 JavaFX Preloader
(3 answers)
Problem With JavaFX/Intellij Setting When Try To Use LauncherImpl For Preloader
(1 answer)
Closed 1 year ago.
I have a JavaFX application which compiles to native using GluonFx plugin.
I want to add a Preloader to show when starting. I managed it by changing the initialization from:
Application.launch(MainView.class, args) to LauncherImpl.launchApplication(MainView.class, MyPreloader.class, args)
The compilation process executes till the end with no errors, but the app doesn't boot.
If I try to run the agent (mvn gluonfx:runagent) I have the following error:
cannot access class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.application to unnamed module
Someone knows how to solve this, or what other way can I use to load my Preloader?
PS: Running from the IDE works. The problem is after compilation to native
Thanks in advance
I've found the solution by using the answer in this ticket:
Java 9 JavaFX Preloader
Replaced:
LauncherImpl.LauncherImpl.launchApplication(MainView.class, MyPreloader.class, args)
by
System.setProperty("javafx.preloader", MyPreloader.class.getCanonicalName());
Application.launch(MainView.class, args);

java: package jdk.incubator.foreign is not visible error in Java 15

So I have downloaded JDK 15 - OpenJDK .
Running in Intelij the following code
import jdk.incubator.foreign.MemorySegment; //The problem seems to occur here in this import
public class Application {
public static void main(String[] args){
MemorySegment m = MemorySegment.allocateNative(400L);
}
}
In inteliJ I have gone to File -> Project Structure -> Project SDK -> selected 15
In Application configurations (Intelij run project) I have declared JRE 15 (java version 15.0.2)
I receive the following error
C:\Users\repositories\java15project\src\main\untitled\src\Application.java:2:21
java: package jdk.incubator.foreign is not visible
(package jdk.incubator.foreign is declared in module jdk.incubator.foreign, which is not in the module graph)
Could someone please help me?
Run with option --add-modules jdk.incubator.foreign
Alternatively, create a module-info.java file, e.g. like this:
module my.module.name.here {
requires jdk.incubator.foreign;
}

Java.lang.NoClassDefFoundError in command prompt run [duplicate]

This question already has answers here:
NoClassDefFoundError: wrong name
(8 answers)
Why am I getting a NoClassDefFoundError in Java?
(31 answers)
Closed 2 years ago.
I'm trying to run my code in command prompt and it gives me error .. Can anyone know whats wrong on it?
Error: Could not find or load main class hello
Caused by: java.lang.NoClassDefFoundError: FirstQuarter/hello (wrong name: hello)
This type of error is due to the class not found in the Classpath during runtime but found during compile time.
Look to print 
System.getproperty("java.classpath")
which will print the classpath so you get an idea as to the actual runtime classpath.
Also, make sure you pass the fully qualified name of the class to "java" command which contains the main method for execution.
directory_that_holds_package>java package_name.Class_name
First, I take a guess that your program could run smoothly in Eclipse and Idea, but it gives this error in command line.
Now, you should include your program's package in command line. If your program is like:
package firstprogram;
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
Then you should run java firstprogram.HelloWorld in FirstQuarter folder.
This error is mainly due to when the program is not able to access the class which you have defined in your program and it can be due to reasons like you have not defined the proper classpath or you have not included the required library needed to run that class. The reasons can be many.
So try to run your code on any IDE as you will be able to easily identify the errors.

java program written in Vscode IDE having different package is not executing properly [duplicate]

This question already has answers here:
Why am I getting a NoClassDefFoundError in Java?
(31 answers)
Closed 2 years ago.
I am getting the following error in VScode IDE .
Kindly help me with the Solutions related to VScode
I created a package named as test in visual studio code .
The program written in that package failed to execute
, program is :
package test;
public class Example{
public static void main(String args[])
{
System.out.println("HELLO");
}
}
It throws an error
the same program works completely fine when placed in the default package here it is
kindly help me with the package working or help me if there is any setting in vscode which has to be setup
The problem is that you cannot run the code from inside the package.
You will have to run the code from Parent Dir of package
EG. If your files,package is structured this way
SomeDirectory
|----test
│ |----Example.class
then you have run it from SomeDirectory by java test.Example

Exception in thread "main" : java.lang.error

I've just created a project on Eclipse and imported some source files (existing project). But I can't compile it ! Ok, the project has got several source files, so I wanted to compile only the Main.java file (with eclipse not in the command line, in the command line it worked!) but all what I get is this error :
http://www.screencast.com/users/Amokrane/folders/Jing/media/82d772dd-10cd-4552-b1d0-3cf18bf39f13
As you can see the Main.java file is straighforward, just a hello world !
What's the matter ?
Thanks
"Unresolved compilation problem" means that the class hasn't compiled successfully. Eclipse will still let you run code that doesn't compile, but any of the specific bits which don't compile will throw this error. Look in the "Problems" tab to see what's wrong.
From the look of the Package Explorer view, every single class has a problem... perhaps the file location doesn't match the package declaration? That would match the position of the pink box just to the right of the vertical scrollbar for the class - it suggests that the error is right at the top of the file, which is where the package declaration would be.
You have a compilation error at the top of your Main.java file, just out of sight in the screenshot. Probably an unresolvable import or a wrong/missing package declaration.
It is simple in my case that the imported project needs 32 bit jre to run but cannot be compiled in the same. In IDE, if you click run, it will try to compile and run the project in single shot so fails due to 32 bit jre for compilation with the above reported error.
So i used 64 bit compiler and started to run and got compiled successfully but thrown error that needs 34 bit jre for some of the SWT used in the project. Again i changed the 32 bit jre and run the project, it is gone ! THE ERROR IS GONE NOW !
You can get the error "Exception in thread "main"
java.lang.Error: Unresolved compilation problem:" if your public class name differs from your file name.
example:
File Name:
ServiceRequest.java
Inside file, class is named differently; like
public class Service

Categories

Resources