Why am I getting sun.misc.Contended symbol cannot find error? - java

I've got a very simple program:
Test.java:
package com;
import sun.misc.Contended;
public class Test {
}
and pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>Test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
</project>
when I put mvn compile I've got:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) on project proj: Compilation failure
[ERROR] /home/john/Desktop/proj/src/main/java/com/Test.java: cannot find symbol
[ERROR] symbol: class Contended
[ERROR] location: package sun.misc
Why?

Because, sun.misc moved to jdk.internal.misc and they are private package to JDK.
... friend interfaces should be moved out of 'sun.misc'
and located in a truly private package. This is so that they are not
part of the proposed to be exported 'sun.misc' package.
You can use --add-exports and --add-opens to gain access to internal API.
In your case, you can add following in your pom.xml
<compilerArgs>
<arg>--add-exports</arg>
<arg>java.base/jdk.internal.misc=<<your.module>></arg>
</compilerArgs>
Please note, you need to change <<your.module>> to your actual module name.
Reference : - JEP-261:Module System

Related

java maven class not found exception

I built a server template in java and now i need to compile and run it using MAVEN.
this is my POM:
<?xml version="1.0" encoding="UTF-8"?>
-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>bgu.spl</groupId>
<artifactId>spl-net</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
-<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<name>spl-net</name>
</project
When I run mvn compile it all works. but then i go to test it with: mvn exec:java - Dexec.mainClass=”bgu.spl.net.impl.BGRSServer.ReactorMain” - Dexec.args=”<port> <No of threads>” and it gives me a class not found exception. I looked in the target directory and this exact main was indeed there, it just refuses to run.
Turns out there was nothing wrong with my code. I failed to realize that in ubuntu, the command: mvn exec:java - Dexec.mainClass=”bgu.spl.net.impl.BGRSServer.ReactorMain” - Dexec.args=”<port> <No of threads>” simply doesn't work. the correct command would be: mvn exec:java - Dexec.mainClass=bgu.spl.net.impl.BGRSServer.ReactorMain - Dexec.args=<port> <No of threads>

JavaFX11 and Intellij

I followed the instruction in this link
https://openjfx.io/openjfx-docs/#install-javafx
and when i run javafx sample i get this error
Error:(3, 26) java: cannot access javafx.application.Application
bad class file: C:\Program Files\Java\OpenJDK\javafx-sdk-11.0.1\lib\javafx.graphics.jar(javafx/application/Application.class)
class file has wrong version 54.0, should be 52.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
and when i go to Project Structure > Project/Modules the JDK is set to JDK11
VM option:
--module-path C:\Program Files\Java\OpenJDK\javafx-sdk-11.0.1\lib --add-modules=javafx.controls,javafx.fxml
I tried to go to Settings > Build, Execution, Deployment > Compiler > Java Compiler
Project bytecode version was set to 8 and i changed it to 11 and the first error was gone but i got new error
Error:java: invalid target release: 11
I was searching on the internet and somebody said go to .idea\compiler.xml and set target to 11 but it's already set to 11 for me but i get the error
Go To File > Project Structure > Modules > Source
Set Language Level to 11 (if 11 not Available set to "X-Experimental Features")
Image
I have just encountered the error, could not find proper solution either, so I figured out myself.
Problem location: pom.xml - maven file
Sometimes issue can happen with different JavaFX entity, for instance with FXMLLoader or EventHandler:
java: cannot access javafx.fxml.FXMLLoader bad class file: ... class file has wrong version 55.0, should be 53.0
java: cannot access javafx.event.EventHandler bad class file: /Users/john/.m2/repository/org/openjfx/javafx-base/12/javafx-base-12-mac.jar!/javafx/event/EventHandler.class class file has wrong version 55.0, should be 53.0
Fixed version of pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hkr</groupId>
<artifactId>CommunicationTest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
</properties>
Incorrect (previous verison):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hkr</groupId>
<artifactId>CommunicationTest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
The issue is in properties tag, the target version was below JDK version (9.0.1), which was causing in my case a problem of "class file has wrong version 55.0, should be 53.0".
I simply changed to target 9 instead of 1.8 and it got fixed.

Whitespace Errors in Maven when Installing Selenium POM

I am trying to follow these instructions to get started with Selenium. I downloaded and installed Maven on my mac but when trying to use mvn clean install with the suggested pom.xml file I get the following errors:
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[FATAL] Non-parseable POM /Users/JB/Documents/Maven/Selenium-Build/pom.xml: only whitespace content allowed before start tag and not { (position: START_DOCUMENT seen {... #1:1) # line 1, column 1
#
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]
[ERROR] The project (/Users/JB/Documents/Maven/Selenium-Build/pom.xml) has 1 error
[ERROR] Non-parseable POM /Users/JB/Documents/Maven/Selenium-Build/pom.xml: only whitespace content allowed before start tag and not { (position: START_DOCUMENT seen {... #1:1) # line 1, column 1 -> [Help 2]
Here is the pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MySel20Proj</groupId>
<artifactId>MySel20Proj</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.13.0</version>
</dependency>
</dependencies>
What am I missing here? I've tried the POM looks like it is structured the same as many of the examples I found when researching this issue. I don't see the whitespace it is complaing about at the start of the document. I am not familiar with Maven, and am really in need of some help.
I've seen this kind of thing happen when copy-and-paste ends up putting a non-printing character at the very beginning of the file, before that initial <. You don't see it - but Maven complains. If you are in Linux, try "cat -v pom.xml" to see if there are any such characters in there...

Locally installed jar not seen by maven

I have source code for a framework, let's call it my-framework. It provides various packages, including com.not_telling.framework.db.
I have a pom.xml file for this framework:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.not_telling</groupId>
<artifactId>my-famework</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>my-framework Maven Library</name>
<url>http://maven.apache.org</url>
<!-- more things here... -->
</project>
I can compile this and install it to my local repository cache, using this command:
mvn install
End of the install log:
[INFO] Installing C:\Users\MyUserName\my-framework\backend\target\my-framework.jar to C:\Users\MyUserName\.m2\repository\com\not_telling\my-famework\0.0.1-SNAPSHOT\my-famework-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\Users\MyUserName\my-framework\backend\pom.xml to C:\Users\MyUserName\.m2\repository\com\not_telling\my-famework\0.0.1-SNAPSHOT\my-famework-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
Then I have another project, that tries to use this as a dependency:
<dependency>
<groupId>com.not_telling</groupId>
<artifactId>my-famework</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
The compilation phase of this project fails with this message:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project my-project: Compilation failure: Compilation failure:
[ERROR] /C:/Users/MyUserName/workspace/my-project/backend/src/main/java/com/not_telling/some_example.java:[13,17] package com.not_telling.framework.db does not exist
Why is that? Maybe because the jar is in the repository cache, but not in a local repository?
This question is a follow up of absolute maven pomderived entry added to classpath - I realized that instead of doing magic with relative source directory references, I need to make a separate module, but I'm facing this problem.
It was bleeding from numerous wounds. These needed to be fixed:
move source files from under src/ to under src/main/java, in both the framework and the application project
change the group and the artifact id to match the package name E.g. for a package that is named "com.not_telling.framework" I had to use groupId=com.not_telling and name=framework.
The dependency scope was wrong in the application's pom file. I had to remove test
Now it works from command line I can do "mvn install" for the framework, and then "mvn package" for the applications.
The only problem left is that the application cannot be compiled from eclipse, I had to remove all linked source folders / build paths in the application project to the framework.

The import com.maxmind.geoip2 cannot be resolved

I added to my web project geoip2 using maven but in the code when im trying to use it, the Import is not working. I can see the jar in maven dependencies but i can't use it. I need some help.
I know it's not a complete answer to your problem, but it's too long for a comment.
I've built a sample project using geoip2. POM is as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>geoip</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>geoip</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>
And the Java code:
package com.test;
import com.maxmind.geoip2.WebServiceClient;
import com.maxmind.geoip2.model.CountryResponse;
public class App {
public static void main(String[] args) throws Exception {
int userId = 0;
String licenseKey = "key";
WebServiceClient client = new WebServiceClient.Builder(userId, licenseKey).build();
CountryResponse response = client.country();
System.out.println(response.getCountry().getName());
}
}
I can't reproduce your import problem. It works just fine. You can download this test here: geoip2-test.zip
Check if you can build your project from command line with: mvn clean install
If so, then maybe it's a misconfiguration problem in your project or IDE.
It's Eclipse? Try Maven -> Update Project. Sometimes this works for me when I add new dependencies that are not immediately detected.
if you use intellij, then first close project and then import project and use auto import for the SBT project.then com.maxmind.geoip2 is resolved.

Categories