I have created a jar file from mvn command but when i am trying to run it i am getting below error :
java -cp target/new-repl.jar package.repl
Error: Could not find or load main class package.repl
Caused by: java.lang.ClassNotFoundException: package.repl
I got many links on stack overflow but none of them helped me. Could you please help me to run this from command line. Below is the my .java file:
package repl;
public class Test {
public static void main(String[] args) throws IOException {
....
}
}
POM.xml:
<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>repl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>repl</name>
<description>build tar for repl</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<build>
<finalName>new-repl</finalName>
<directory>target</directory>
</build>
</project>
You can do like this. Type fully qualified class name instead of typing package name
java -cp target/new-repl.jar repl.Test
The error says it all
Error: Could not find or load main class package.repl
your class name is not package.repl, it is repl.Test
Related
Trying to use this library, but whenever I run it. It gives me the following. I updated my pom file and thought it would be the solution, but I still receive an error.
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jna/Pointer
at org.example.Main.main(Main.java:11)
Caused by: java.lang.ClassNotFoundException: com.sun.jna.Pointer
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more
Here is my code
public class Main {
public static void main(String[] args) throws WinDivertException {
System.out.println("Hello world!");
WinDivert w = new WinDivert("tcp.DstPort == 80 and tcp.PayloadLength > 0");
w.open(); // packets will be captured from now on
Packet packet = w.recv(); // read a single packet
System.out.println(packet);
w.send(packet); // re-inject the packet into the network stack
w.close(); // stop capturing packets
}
}
And here is my 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>org.example</groupId>
<artifactId>Glacial1.1</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.github.ffalcinelli</groupId>
<artifactId>jdivert</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Tried updating my pom file and it didn't work
I'd like to use JBoss Weld as a CDI facility in a classic Hello World application in a maven structured project. To keep the things as clean & simple as possible, I only created a Weld environment object and nothing more. I also created the deployment descriptor file beans.xml in src/main/resources/META-INF and src/test/resources directories. I did't go further by creating and initializing a WeldContainer etc. I'm just wondering why this settings don't work in the first place.
The application compiles perfectly and generates an executable jar file with mvn package command. However I got a runtime error:
C:\dev\eclipse-workspace\my-app>java -cp target/my-app-1.0.jar app.Hello
Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/weld/environment/se/Weld
at app.Hello.main(Hello.java:7)
Caused by: java.lang.ClassNotFoundException: org.jboss.weld.environment.se.Weld
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.j
ava:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoader
s.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more
So, here is my main class:
1 package app;
2
3 import org.jboss.weld.environment.se.Weld;
4
5 public class Hello {
6 public static void main(String[] args) {
7 Weld weld = new Weld();
8
9 System.out.println("Hello World");
10
11 weld.shutdown();
12 System.exit(0);
13 }
14 }
my 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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jee</groupId>
<artifactId>my-app</artifactId>
<version>1.0</version>
<name>my-app</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>3.1.3.Final</version>
</dependency>
</dependencies>
</project>
and lastly my project structure:
Your help is appreciated in advance.
Thank you.
Alex
You will note that java -cp target/my-app-1.0.jar app.Hello does not include the Weld jars on the classpath (unless your jar file's META-INF/MANIFEST.MF has a Class-Path entry that references them). That is why Weld classes cannot be found at runtime when you start your application in this manner.
This question already has answers here:
How do you specify the Java compiler version in a pom.xml file?
(5 answers)
Closed 5 years ago.
This is my simple code from a Maven project which has try-with-resources.I use eclipse as my IDE.
public class Hello {
public static void main(String[] args) {
try(FileInputStream fi = new FileInputStream("ANC")){
} catch (IOException e) {
e.printStackTrace();
}
}
}
When I build this using clean package -U it gives me the following error.
Hello.java:[11,20] try-with-resources is not supported in -source 1.5
(use -source 7 or higher to enable try-with-resources).
However,I have my java compiler to be set at Java 1.8 and in Jave Build path JRE System Library is also JDK 1.8.Still this error persists.
This is my POM file ( minus the junit dependency )
<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.commonutil</groupId>
<artifactId>PropertyFile</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>PropertyFile</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Note : Setting the target and source in pom.xml did not help either.
Any help regarding this is appreciated.Thank you.
You need to specify the java source version, so the correct flag value is used by the compiler plug-in. For example:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
I have followed the tut here http://sparkjava.com/documentation.html#getting-started
pom.xml
<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>spark</groupId>
<artifactId>Spark</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spark</name>
<description>spark</description>
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</project>
Hello.java
import static spark.Spark.*;
public class Hello {
public static void main(String[] args) {
get("/hello", (req, res)-> "Hello World");//eclipse error this line
}
}
I got an error with eclipse
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
req cannot be resolved to a variable
Syntax error on token ",", . expected
Syntax error on token "-", -- expected
at Hello.main(Hello.java:5)
Java's lambda expressions are a feature in Java 8: Oracle link. You'll need to change your compliance level (and java version, and pom) to java 8 to make this example compile.
I am going to deploy application with JaCoCo agent to production environment to let it work for some time. The result should help me identify the parts of code I can get rid of.
I started some research around the topic and prepared HelloWorld application:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
Then I compiled the class: "javac HelloWorld.java" and got HelloWorld.class file.
Now I run the app with the following command: "java -javaagent:jacocoagent.jar HelloWorld" the program executes and jacoco binary is generated. The file contains some binary data.
Everything looks fine but the coverage report shows 0% coverage although it should be 100%.
Has anyone faced this issue or correct me what I am doing the bad way?
I generated full report using this steps. Since I use maven for this kind of operations I added maven after your steps. I created HelloWorld.java just copying from your question. Then I follow these steps:
javac HelloWorld.java which outputs HelloWorld.class
Then I created jacoco.exec by executing java -javaagent:jacocoagent.jar HelloWorld
Then I created a pom.xml file which contents are like this.
<?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>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>test</name>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
</plugin>
</plugins>
</build>
After that I created a target/classes directory. I copied jacoco.exec to target/ and HelloWorld.class to target/classes.
Then I executed mvn jacoco:report which generates a report to target/site/jacoco. Which contains correct coverage information.
I know using maven may not sound good for a simple application. But I don't know any other way to generate reports from jacoco.exec. By the way your maven plugin version and jacocoagent version must match.
And here the result I get.