java maven class not found exception - java

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>

Related

Run a simple Java HelloWorld with Heroku Scheduler

I want to run a simple program -- like HelloWorld -- on Heroku, and also using Heroku Scheduler to run each 10minutes.
I`m not sure about Maven-pom.xml setting, Heroku run task system, and Heroku Scheduler.
Now I have these source but the task result is failed.
> MyApp.java
package com.github.graycat27.sample
public class MyApp {
public static void main(String[] args){
System.out.println("Hello Heroku Scheduler task!");
}
}
> 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.github.graycat27</groupId>
<artifactId>MyApp</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
</project>
> Heroku Scheduler job command
$ java MyApp
deploy to Heroku via GitHub has completed in success.
as a result, logs shows
2021-07-21T11:13:59 app[api]: Starting process with command `java MyApp` by user scheduler#addons.heroku.com
2021-07-21T11:14:02 heroku[scheduler.5096]: Starting process with command `java MyApp`
2021-07-21T11:14:03 heroku[scheduler.5096]: State changed from starting to up
2021-07-21T11:14:05 app[scheduler.5096]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -XX:CICompilerCount=2 -Dfile.encoding=UTF-8
2021-07-21T11:14:05 app[scheduler.5096]: Error: Could not find or load main class MyApp
2021-07-21T11:14:05 heroku[scheduler.5096]: Process exited with status 1
2021-07-21T11:14:05 heroku[scheduler.5096]: State changed from up to complete
what should I change my program or config?
Your ClassName isnt MyApp its com.github.graycat27.sample.MyApp and you need to provide where it can find this File on an Class-/Module-Path

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.

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

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

maven-compiler-plugin:3.1:compile(1 errors) in eclipse [duplicate]

This question already has answers here:
How to force maven update?
(26 answers)
Closed 6 years ago.
I want to learn java spring, so I choose Eclipse Mars2 Version and I want to build a java project using Maven. But I get errors
maven-compiler-plugin:3.1:compile(1 errors)
maven-compiler-plugin:3.1:testCompile(1 errors)
Then I check my maven and java path in my Windows 10 64 bit system variable.
I configured like this :
JAVA_HOME => C:\Program Files\Java\jdk1.8.0_71
M2_HOME => C:\apache-maven
MAVEN_HOME => C:\apache-maven
And in systen variabel path :
%JAVA_HOME%\bin
%M2_HOME%\bin
Everything is fine, I checked it by java -version and mvn-version
Also, I have installed mvn plugin in eclipse in install new software
to maven :
Name: m2e
Location: http://download.eclipse.org/technology/m2e/releases
But I still failed. The errors is still pop up.
Please advise. Thanks.
UPDATE
I test to build a project maven-archetype-quickstart version 1.1
execution default-compile, in Access/pom.xml
execution default-testCompile, in Access/pom.xml
UPDATE AGAIN
<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.tresnamuda</groupId>
<artifactId>access</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>access</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Eclipse detect error in first line which is :
project xmlns="http://maven.apache.org/POM/4.0.0" .....
also in line 8.
This is the complete error :
It seems like there is corruption in your local repository.
Please try:
Delete the local repository, if you use Windows, delete the folder C:\Users\your-user-name\.m2\repository
In Eclipse, right click on the project name and click Maven\Update project ... (or use hotkey ALT+F5)

Out-of-tree build with maven. Is it possible?

I start learning packaging for several distros (currently Cygwin and Debian).
They have requirement to build system to allow out-of-tree build (synonym out-of-source build):
http://wiki.debian.org/UpstreamGuide#Out-of-Tree_Builds
To work-around "dumb" build system for example cygport recommend use lndir (from xutils project):
lndir ${S} ${B}
cd {B}
...build-commands...
I read mvn(1) man page but doesn't found anything appropriated. Next I just try:
$ mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
...
$ pwd
/maven/simple
$ ls
my-app
$ mvn -f my-app/pom.xml compile
...
[INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) # my-app ---
[INFO] Compiling 1 source file to /maven/simple/my-app/target/classes
As you can see target directory created in source root hierarchy while I look for a way to avoid this.
Is it possible out-of-tree build with maven? And how?
You could do like this to get it in your current working directory:
<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.stackoverflow</groupId>
<artifactId>Q13173063</artifactId>
<version>1.0-SNAPSHOT</version>
<name>${project.artifactId}-${project.version}</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<buildDir>${user.dir}</buildDir>
</properties>
<build>
<directory>${buildDir}</directory>
</build>
</project>
Then you can issue
mvn -f my-app/pom.xml compile
And it will give you your classes in the current working directory.
And easily change to another output directory:
mvn -f my-app/pom.xml -DbuildDir=/tmp/build compile
It might be as simple as having a
<build>
<directory>/your/build/directory</directory>
</build>
in your pom.xml. /your/build/directory need not be in the source tree and can be parameterized using the usual ${...} syntax.
Cheers,

Categories