eclipse error new to code please - java

so I coded a little thing to try and I don't know why eclipse isn't running my code. Please help I'm new to coding. Error occurred during
import java.util.GregorianCalendar;
import java.util.Scanner;
public class Calendar {
public static void main(String []args) {
GregorianCalendar cal1 = new GregorianCalendar();
Scanner input = new Scanner(System.in);
System.out.println("Please enter your hire year (yyyy): ");
int hireYear = input.nextInt();
int currYear = cal1.get(GregorianCalendar.YEAR);
System.out.println("He's worked here for " + (currYear - hireYear) + "years!");
}
}
initialization of boot layer
java.lang.LayerInstantiationException: Package jdk.internal.jimage.decompressor in both module java.base and module jrt.fs

this seems to happen when there's multiple versions of java (previous to 9 and 9+)
see this bug report for eclipse
maybe try using another IDE or use command line for compiling and running

Are you using Eclipse Photon? This isn't officially released yet (but I've found it quite stable). It does look like Bug 532490, which is a very recent regression and it will be fixed in the next ~ 6-weekly milestone update.
In the meantime, right-click your project > Build Path > Configure Build Path > 'Libraries' tab. Double-click "JRE System Library" and change it from 8 to 10. Build & run and the problem should be gone.
As a rule it is good to compile and run using the same JRE/JDK. It is possible to mix and match these, but I would say it should only be done in certain "advanced usage" scenarios.

The only thing that worked for me when I had this error was uninstalling then reinstalling Eclipse. Then I made sure to make all future java projects with java version 1.8 (when creating a java project, under JRE, select "use an executing environment JRE:", then select "JavaSE-1.8"). Hope that helps!

Related

'Could not find or load main class' in Eclipse. Again and again

I'm asking this question even though it has already been asked many times because I didn't find a solution after hours of searching.
I'm using Eclipse to learn Java. Last week everything worked properly. I could run whatever I wrote. Then today, I opened Eclipse, wrote some lines and when I tried to run it, I got an error:
Error: Could not find or load main class test.ArrayTest
Caused by: java.lang.ClassNotFoundException: test.ArrayTest
But that's not all : every project in my workspace is having the same issue. I can't run anything. At first I thought it was because I made a mistake at Eclipse start, so I closed it and re-opened it, but I was in the correct workspace.
I've tried to clean/rebuild it, doesn't work.
I've tried to mess with Properties > Java Build Path, doesn't work.
I've tried to create a new project with a simple 'Hello World', doesn't work.
package Hello;
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
What I have in package explorer.
Console display.
The thing that suprises me the most is that I don't think I've done anything that can mess with Eclipse between the moment it was working and now.
Does someone have another idea ?
I think I found the root of the problem !
The path to my workspace was containing a letter with an accent ('Bibliothèque' by default with Windows 7+ in french...), so I tried to create a new workspace outside of it, and it worked !
I'm not a 100% sure it was really it, but since it worked, it's worth sharing.
Probably you have disconected JDK in project.
Try open conextual menu on project (right mouse button)
properties/Java Build Path/Libraries
check is correct JDK or remove
then: [Add Library] / JRE System Library/ Alternate JRE/ [Installed JREs] / [Add] / Standard VM / [directory]
select path to your JDK (not JRE) and confirm, select checkbox your new jdk and [Apply and close]
on select list choose your new jdk and [Finish] and [Apply and Close]
should works. I have sometimes this problems. It's Eclipse

IntelliJ Idea Completion Error

I am new to Idea IDE. After installed it for a few days, I was fascinated with its outstanding features.
However, while I was actually writing code yesterday. I discovered that when the auto completion popped out, it showed nothing about the method of the object , merely some basic template say sync() var() ..... etc.
I have tried to setup the environment (Check the power save ...etc), but those previous solution seems to be for those whose auto-completion is turned off . Is there a way to fix it ?
THANKS IN ADVANCE!!
When you have create your project, you might not have marked it as a Java project and provide a SDK.
You can check it in File -> Project Settings -> Project -> Project SDK.
If not, you can download the JDK on the Oracle site http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
You can also try to write a
public static void main(String... args) {
System.out.println("It works !");
}
and write click on the "main" word. You should have a "Run 'Main'" option.

println cannot be resolved as a variable

I am studying object oriented programming in my university and my professors is making an example of streams on java. Unfortunately when I try to run this code it says "println cannot be resolved as a variable" on System.out::println.
It also says to delete the two colons,
while on my professor's eclipse it does work.
This is the code. I am running it on eclipse for ubuntu and my version of java is 1.8 update 91
package stream;
import java.util.Arrays;
public class StreamExamples {
public static void main(String[] args){
String text = "I have no idea what is the problem";
String[] words = text.split(" ");
Arrays.stream(words)
.sorted()
.limit(3)
.forEach(System.out::println);
}
}
Most likely the version of Eclipse you are running does not understand Java 8 syntax.
You need to be running the current Eclipse 4.6 (Neon) or the previous release 4.5 (Mars) for full Java 8 support. You code works fine on Eclipse Neon.
1) Check your build path by right clicking on your project, Build Path > Configure Build Path. The libraries tab should show a JRE with version 1.8, if that is not the case, click Edit and select a JRE that is at least version 8.
2) Check the compiler settings by right clicking on your project, Properties > Java Compiler. Change the Compiler compliance level to 1.8.

Uncompilable source code - Erroneous sym type:

There was error occurred in Line 14.
Exception in thread "main" java.lang.RuntimeException: Uncompilable
source code - Erroneous sym type:
Hard to find solution.
package example;
public class Num
{
public static void main(String [] args)
{
String s = "42";
try
{
s = s.concat(".5");
double d = Double.parseDouble(s);
s = Double.toString(d);
int x = (int) Math.ceil(Double.valueOf(s).doubleValue()); //Line 14
System.out.println(x);
}
catch (NumberFormatException e)
{
System.out.println("Wrong Number");
}
}
}
The code you provided runs fine on my computer.
I'm guessing you are using Netbeans and may be affected by a bug.
Try this:
Open the project properties, select the Build-Compiling, uncheck "Compile on save" and rerun the application. This will make sure all your source code becomes recompiled before running it.
Link: https://forums.netbeans.org/topic43241.html
This is a common issue which I get while working. What I do is clean and build the project. It solves the problem.
Right Click on the project name >> Clean and build
Download/install JDK major version number the same as NetBeans.
i.e. If You have NetBeans 9 version, but your JDK is version 10,then try use NetBeans 10 version... or download/install JDK 9 (the same as NetBeans).
Create new java platform for new jdk installation.
Open netbeans menubar->tools->Java Platforms->Add Platform
Specify the folder that contains the Java platform as well as the sources and Javadoc needed for debugging.
Set new java platform to your project
Right click on project ->properties->Libraries->Java Platform->select the jdk version that same as NetBeans
Select source format of your project
Right click on project ->properties->Sources->Sources/binary format->select the jdk version that same as NetBeans.
If it still gives errors like,
Exception in thread "main" java.lang.RuntimeException:
at javaapplication2.JavaApplication2.main(JavaApplication2.java:1)
C:\Users\James\AppData\Local\NetBeans\Cache\dev\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\James\AppData\Local\NetBeans\Cache\dev\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 1 second)
do as "sinclair" has mentioned above.
Open the project properties, select the Build-Compiling, uncheck "Compile on save" and rerun the application. This will make sure all your source code becomes recompiled before running it.
I received this error after I'd changed the type of a function parameter and it was the calling page that was in error - but I didn't see this until a magical Clean and Build pointed me to the actual issue!
I got this error message because I copied and pasted code without including the package name. I added the package name and it was fixed.
in my case
i have solved using import with package name and class name
like this import package.class1;
Please uncheck "Compile on save"

The "import java.util.function cannot be resolved" error

I'm trying to run this practice script from the standard Oracle Java tutorials.
This seems to be a common error and I've used SO resources to make attempts to fix this. I've tried Cleaning the project, refreshing the project, switch workplace and switch back, removed and re-added the JRE7.
I don't know what else to do.
import java.util.List;
import java.util.function.Consumer; -----> cannot be resolved ERROR
import java.util.function.Function; -----> cannot be resolved ERROR
import java.util.Comparator;
import java.util.function.Predicate; -----> cannot be resolved ERROR
import java.lang.Iterable;
import java.time.chrono.IsoChronology; -----> cannot be resolved ERROR
public class LambdaExpressions_RosterTest {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Per the java.util.function Javadoc,
Since:
1.8
So upgrade to Java 8, or try to find an older version of the tutorial.
I'm new at this. How can you tell what you are running? I'm using Eclipse
To determine your current Java version in eclipse, go to
Help -> About Eclipse -> Installation Details (Button in
lower Left) -> Configuration pane
Look for the line java.specification.version - on my machine that is
java.specification.version=1.8
Or the line java.runtime.version - on my machine that is
java.runtime.version=1.8.0_11-b12
i solved this problem by trying following solution
Project > Properties > Java Build Path
Select Libraries tab
Select JRE System Library
Click Edit button
Choose an alternate JRE (jre1.8.0_20)
Click Finish button
Lambda Expressions are newly added into Java 8. They are not available for JRE7.
Try to upgrade your eclipse project's JRE to 8 (window -> preferences->java->compiler).
For idea IntelliJ you need to go to Menu-> Project Structure -> Module here you will see Source tab, above this tab you can see language level. change it to more than 8.
-yash
I had something similar with Apache Camel:
the following function was not working anymore
String headerKey = exchange.getIn().getHeader("headerKey",String.class);
because of the error-message:
The type java.util.function.Supplier cannot be resolved. It is indirectly referenced from required .class files
To solve the issue I change the casting and it worked afterwards:
String headerKey = (String) exchange.getIn().getHeader("headerKey");
maybe it helps you or somebody else.
One other point to mention here would be the Apache Camel version itself as stated on the official page linked here.
same problem is coming to me,I make a changes in eclipse.ini file and
did -Dosgi.requiredJavaVersion=1.8 instead of -Dosgi.requiredJavaVersion=1.6
and my problem has been resolved.
Ran into this problem when the version I was using in the below properties was <1.8. Updating my POM to a version >1.8 resolved my issue.
<properties>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
I ran into this while trying to use Java 9 with Eclipse Oxygen. Eclipse claimed that I was using less than Java 1.8 and asked if I wanted to use 1.8. Saying yes solved the problem, but led to this error message later on.
I went in to Window => Preferences => Java => Compiler and saw that this had been changed to 1.8, so I changed it back to 9 and everything seems to be working now.

Categories

Resources