Intellij can't find java.net.http when compiling with Java 11 - java

I'm trying to get one of my projects ready for Java 11 but for some reason Intellij can't find java.net.http. It isn't underlining it as not found in module-info.java like it would if I typed it wrong but when I try build the project I get the error below. I've tried reinstalling Intellij 2018.2.3 and uninstalling all other versions of Java. Any advice on how to get this working would be appreciated.
Error:
Information:java: Errors occurred while compiling module 'crawler'
Information:javac 11 was used to compile java sources
Information:15/09/2018 11:16 - Compilation completed with 1 error and 0 warnings in 636 ms
C:\Users\Will\IdeaProjects\crawler\src\module-info.java
Error:(2, 22) java: module not found: java.net.http
module-info.java:
module crawler {
requires java.net.http;
}
Request.java:
package Request;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Request {
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("starting download");
String body = HttpClient.newBuilder().build().send(HttpRequest.newBuilder().uri(URI.create("https://example.com")).build(), HttpResponse.BodyHandlers.ofString()).body();
System.out.println("finished download:" + body);
}
}
Structure:
crawler
src
Request
Request.java
module-info.java

In the case that the above proposed resolution (by #Will) does not solve your issue as was the case with me (i.e. setting the project language level), check to to see what the bytecode target version of your java compiler has been set to, in your project preferences:

I had the wrong project language level set. To use java.net.http you need it to be at least 11. To change the project language level see: https://www.jetbrains.com/help/idea/project-page.html
Hopefully this helps someone else out.

I had the same problem with the package jdk.jfr.
This is how I fixed it. It should work for you too.
In order to make it work I had to make 2 changes:
First I had to set the language level to 11; see in the picture below.
Then I had to adjust the Java Compiler. The Target bytecode version is 11 and I set the project bytecode version Same as language level. Then you don't have to change all of them constantly. Please see picture below.

For those who are having this problem in 2022, even if the solutions mentioned here did not help, I was able to figure out what the problem was and how to fix this.
First of all I wanted to make sure that the problem is not from my Maven config, so I ran the following in my terminal:
mvn package
followed by:
java -cp target/covid-cases-cli-1.0-SNAPSHOT.jar org.matrixeternal.covidcasescli.App
and it was build without any errors whatsoever. So it means something is up with IntelliJ.
I am using Java 17 and building with Maven using IntelliJ. IntelliJ uses its own internal command to build the project. To override this behaviour, you must go to Preferences - Build, Execution & Deployment - Build Tools - Maven - Runner and select the option Delegate IDE build/run actions to maven which will essentially run directly from the Maven config file using the mvn tool installed in the system, instead of using the IDE command.

Set the compiler for IntelliJ as Java 11
IntelliJ Idea-> Preferences-> Build, Execution, Deployment -> Java Compiler
Select java 11 from the drop down

Related

How do I resolve "the type is not accessible" error in Eclipse when using an external JAR? JAR in question is algs4 from Princeton [duplicate]

I'm new to the whole programming stuff but here's my problem:
I used to add my JUnit test cases in Eclipse by right clicking on the project, and just add New > JUnit Test Case.
Currently, I am not able to implement any test methods because Eclipse tells me on the line
import static org.junit.jupiter.api.Assertions.*;
the error message
The type org.junit.jupiter.api.Assertions is not accessible.
Error I get in the IDE:
I tried the following:
Reinstalling Eclipse, using a fresh workplace.
Adding the JUnit to Build path
Nothing helped.
It worked and works in older projects just fine.
Here is how the Package Explorer looks:
What am I missing?
You use the Java Platform Module System (JPMS) by having a module-info.java file in the default package probably without the required requires <module>; statement. JPMS was introduced in Java 9.
Do one of the following:
Delete the module-info.java file (if needed, you can recreate it via right-clicking the project folder and choosing Configure > Create module-info.java)
In module-info.java add the corresponding requires statement, e.g. by going to the line with the import statement and using the corresponding Quick Fix (Ctrl+1)

Using Jsoup library on eclipse [duplicate]

I have just recently started using Eclipse and am running into problems trying to install external libraries. Following online tutorials, I add the .jar file to the classpath and I see it in the referenced libraries folder. Despite this, when trying to import, I get the error:
The package org.apache.commons is not accessible
For reference, I am trying to install the apache math commons library.
Your code probably has two issues.
First, the import statement is wrong since in Java you cannot add a package itself, but all classes of a package as follows (note .*; at the end):
import org.apache.commons.math4.linear.*;
or a specific class, e.g.
import org.apache.commons.math4.linear.FieldMatrix;
Second, you use the Java Platform Module System (JPMS) by having a module-info.java file in the default package probably without the required requires <module>; statement. JPMS was introduced in Java 9 and you have Java 12.
Do one of the following:
Delete the module-info.java file (if needed, you can recreate it via right-clicking the project folder and choosing Configure > Create module-info.java)
In module-info.java add the corresponding requires statement, e.g. by going to the line with the import statement and using the corresponding Quick Fix (Ctrl+1)

Eclipse throws the error "Data cannot be resolved to a type" for using the Project Lombok [duplicate]

This question already has an answer here:
Eclipse Java IDE JUnit5: junit.jupiter.api.Assertions is not accessible
(1 answer)
Closed 2 years ago.
I am new to Java and Project Lombok, I am trying to follow some tutorials to understand it better. I have created a very simple java project to test the Project Lombok and it throws the error Data cannot be resolved to a type when I add the annotation #Data to my class.
I am using macOS and Eclipse IDE for running my Java application. Following are the steps that I followed to run my first Java and Lombok project:
Downloaded the latest Project Lombok version 1.18.16 and placed it in the Documents folder.
Installed the Project Lombok in my system using the command java -jar lombok.jar.
Restarted my Eclipse and verified that Project Lombok installed in the Eclipse -> About Eclipse: "Lombok v1.18.16 "Envious Ferret" is installed. https://projectlombok.org/"
Created a new Java project and the class Alien.
Added the Project Lombok JAR to my project Classpath.
Added the #Data annotation to my Alien class and imported the JAR file but I get the error The type lombok.Data is not accessible for my import statement import lombok.Data;
Also, I get the error Data cannot be resolved to a type for my annotation at the class level public #Data class Alien
I am really unsure what's wrong. I tried to find out but all the answers have mentioned the same steps. I was just wondering what am doing wrong. Any help would be really appreciated.
Following is my complete code:
package com.lamboktest;
import lombok.Data;
public #Data class Alien {
private int age;
private String name;
}
The error The type SOMETYPE is not accessible is... bizarre. As far as I know, there are only three ways to get that error:
lombok.Data isn't public. Except, it is, so this can't be it.
You've manually added a list of forbidden packages ('access rules') in eclipse, to deny yourself access to lombok. Presumably, you'd remember if you did that, so it can't be this.
lombok is loaded as a module, and the lombok package isn't exported. Except, the only versions of lombok that exist either aren't modularized (and module-info-less jar files export everything), or they are, and the lombok package is exported. So it can't be this.
Perhaps it would be useful to doublecheck all of these:
I guess javap -cp lombok.jar lombok.Data, and check that it's public, but, this just cannot be it, so you probably don't need to bother.
For that access rules thing, which is the most likely of 3 very unlikely options, go into your project's setup (right click on it in e.g. the package explorer, and pick 'properties...', you can set up lots of stuff from there), go to 'build path', find the lombok.jar, open it up, and check 'access rules'. It should read 'no rules defined'.
Go into your project's setup and ensure it's all set to java8, and rebuild. Presumably you want to use new features, but the point is to at least isolate down to: It's a module issue.
Posting the answer as it maybe useful for somebody in the future.
As mentioned in the comment removed/deleted the file module-info.java which was created by default during the project creation. Everything worked as expected.

Java 9 migration on intellij issues with module system

I am try to migrate my current project to be able to compile and run it on java 9. I am trying first to just move a java8 based project to java9 without not much effort which means not moving to jigsaw basically.
The project structure is something like
myjava-service [myjava-service-parent] parent-pom with the following modules
- myjava-service
- myjava-service-common
- myjava-service-test
it compiles perfectly with mvn clean package and it runs when I execute that fat.jar
the nightmare starts when I try to run it using intellij,to run on intellij i have to set the module I want to execute which is myjava-service but then apparently intellij understand it as java 9 module and well a lot of split packages issues, classes not found and other issues that I am struggling to fix, so my question is there a way to run the service on intellij under the java 9 environment without the new java module system being triggered somehow?
for the record issues like
java.lang.module.ResolutionException: Modules javax.annotation.api and annotations export package javax.annotation to module javax.el
So, apparently someone stepped into the same issue that I was facing, so what happens is that when your service start by a class which is not part of your sources (in my case my service is started by Starter class from the Vertx jar) then IDEA end up using the module path instead of class path. luckily Intellij team was quite helpful, follow up the ticket
https://youtrack.jetbrains.com/issue/IDEA-187390 , for the next version IDEA will abstain from using module path when there is no module-info file in sources.
Also for those who need desperately run a service in java 9 there's a work around which is basically create a Main class and Invoke it inside the class that starts your service, in my Case it looked like
public class Start {
public static void main(String[] args) {
Starter.main(args);
}
}

Why do i get a java.lang.NoSuchMethodError: even though the build is green?

I wrote a test using HtmlUnitDriver in intellij.
The code is:
import junit.framework.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class HtmlUnitDemo {
WebDriver driver;
public static void main(String args[]){
HtmlUnitDemo obj = new HtmlUnitDemo();
obj.setup();
obj.openURL();
}
public void openURL() {
driver.get("https://mail.google.com");
driver.findElement(By.id("username")).sendKeys("xyz");
driver.findElement(By.id("password")).sendKeys("pqr");
driver.findElement(By.className("btn-submit")).click();
Assert.assertEquals(driver.getTitle(), "CAS – Central Authentication Service");
}
public void setup(){
driver = new HtmlUnitDriver();
}
}
This code builds when i use gradle. But, when I run it in the IDE, it gives me a
*java.lang.NoSuchMethodError: org.apache.http.conn.scheme.Scheme.<init>(Ljava/lang/String;ILorg/apache/http/conn/scheme/SchemeSocketFactory;)
The entire stack trace is:
at com.gargoylesoftware.htmlunit.HttpWebConnection.createHttpClient(HttpWebConnection.java:504)
at com.gargoylesoftware.htmlunit.HttpWebConnection.getHttpClient(HttpWebConnection.java:470)
at com.gargoylesoftware.htmlunit.HttpWebConnection.setUseInsecureSSL(HttpWebConnection.java:659)
at com.gargoylesoftware.htmlunit.WebClient.setUseInsecureSSL(WebClient.java:1085)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.createWebClient(HtmlUnitDriver.java:263)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:129)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:172)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:168)
at smoke.com.thoughtworks.twu.HtmlUnitDemo.setup(HtmlUnitDemo.java:30)
at smoke.com.thoughtworks.twu.HtmlUnitDemo.main(HtmlUnitDemo.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
I googled this error and it said something related to class path. But I am not sure what exactly is to be done. I am very new to this field. So any help will be deeply appreciated.
Thank you in advance.
NoSuchMethodError might indicate that the library where the class org.apache.http.conn.scheme.Scheme is has a different version that what is expected.
Check the dependencies used by the Selenium library you are using. If it's from maven it can be a simple task, otherwise check on their website.
I had this same error and was very difficult for me to solve.
java.lang.NoSuchMethodError: org.apache.http.conn.scheme.Scheme.<init>(Ljava/lang/String;ILorg/apache/http/conn/scheme/SchemeSocketFactory;)
There are two classes on my Classpath that have the same "fully qualified" name
org.apache.http.conn.scheme.Scheme
Java simply picks the first Scheme class is sees and it's the wrong one. It uses the Scheme class that doesn't have the method I expect.
Ok so I fix by just declaring the right dependency in Maven right? No. Both of the Scheme classes are in different Artifacts, and one is a transitive dependency so I can't remove completely.
Solution We had to manually override Maven by putting the Jar of the Scheme class we want to use inside Tomcat. The dependencies picked up by the Tomcat container are loaded before the Maven dependencies. So load the Scheme class Jar into your web container so you can guarantee it's found first.
These commands were very useful while analyzing.
mvn dependency:analyze -o
mvn dependency:tree -o
If you are using IntelJ IDE. Make sure you are giving right JDK in the project detail. You can check it by
Press Ctrl+Alt+Shift+S -> Will open Project Settings.
click on project. You can see which JDK you are specified there.
Go to your path of JDK in your system. If you are on Windows 64 bit version. it will be
C:\Program Files\Java\
Make sure The JDK you have specified also have the corresponding jre in the same path(JDK,JRE version should be same).
So you can see I have jdk1.8.0_141, jre1.8.0_141. And I have Indexed This jdk1.8.0_141 to my IntelJ.
When you are checking If you see like Corresponding JRE not present. IntelJ indexing will not be proper and it lead to such errors which you mentioned in the question.
You can download latest JDK from http://www.oracle.com/technetwork/java/javase/downloads/index.html
If you are on windows use .exe version of JDK and run in your system. Which will automatically install corresponsind jre in the same path.
You can put your doubts on comments. Will try to help you out.
After that come back to IntelJ. Go project Settings->projects. Click New and select and add the proper JDK. Click OK. Then Go to your program. you can see IntelJ Is reindexing your things. May take couple of minutes. After that try to re run the program.

Categories