I'm coming back to my origin of Java development. I'm use to work in C# with Nuget packages. I have basic notion of Maven. I would like to use VSC (Visual Studio Code).
I'm looking how I can import my first package to use a function/method ByteUtils. It seems this method come from an Apache package.
So here is my 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>testGroupId</groupId>
<artifactId>artifactId</artifactId>
<version>1.0</version>
<name>artifactId</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>21</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
And a screenshot with the issue:
The container 'Maven Dependencies' references non existing library 'C:\Users\xxxx.m2\repository\org\apache\apache\21\apache-21.jar'
Missing artifact org.apache:apache:jar:21
To be complete here is my app.java
package testGroupId;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class App
{
public static void main(String[] args) throws Exception
{
MessageDigest md = null;
try
{
md = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException e)
{
System.out.println(e.getMessage());
}
//byte[] theDigest = md.digest(ByteUtils.intToBytesBigEndian(123456789))
System.out.println("Hello Java");
}
}
My question. Why ny org.apache dependency cannot be loaded or downloaded?
The artifact org.apache:apache:pom:21 is not a JAR artifact instead it is a POM artifact which can be used as a parent for you maven module or can be used in the depencyManagement section.
If you add a POM artifact as normal dependency maven will try to download the jar file for the artifact but it doesn't exist thus the error.
To add as a parent
<parent>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>21</version>
</parent>
To add it in the dependencyManagement
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>21</version>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
If you want to org.apache.commons.compress.utils.ByteUtils then you have to add the following dependency.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
</dependency>
Related
Not getting java support after creating maven project in eclipse
when maven project created I am not getting src/main/java,src/test/java and jre system libraries folders. i am following steps to create new project
steps to create
** maven project: File-->new-->project-->select maven project-->next-->select create a simple project-->next-->give project id and artifact id-->finish.**
<?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>MavenProject1</groupId>
<artifactId>MavenProject1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MavenProject1</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Yes, you wont get that. Its just a basic, very simple maven project that eclipse creates for you, so that you can structure the project as you like.
You now have 2 options:
create all the structuring by yourself
Create a new maven project and instead of selecting select create a simple project, select from archetype, and from there you can you can select what you like (Ex: maven-archetype-quickstart ) according to what you like.
For more info on setting up maven project with arch type search in youtube. You will get a lot of video tutorials.
I have created a simple SpringBoot project using maven and my only class present is App.java containing
package com.mycompany.app;
// import org.slf4j.Logger;
// import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
#RestController
public class App {
// Logger logger = LoggerFactory.getLogger(App.class);
#GetMapping("/test-docker")
public String getData() {
return "in docker tutorial project of example";
}
public static void main( String[] args )
{
// System.out.println("SOP method");
SpringApplication.run(App.class, args);
}
}
This 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
</dependency>
</dependencies>
<build>
<!-- <pluginManagement>lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.mycompany.app.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!-- </pluginManagement> -->
</build>
</project>
When I do a mvn install, the jar file is created in my target directory. When I run the following command,
java -jar target/my-app-1.0-SNAPSHOT.jar
I get the error:
lrd72218673:my-app subnara$ java -jar target/my-app-1.0-SNAPSHOT.jar
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.lang.IllegalArgumentException: Unable to load factories from location [META-INF/spring.factories]
at org.springframework.core.io.support.SpringFactoriesLoader.loadSpringFactories(SpringFactoriesLoader.java:151)
at org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames(SpringFactoriesLoader.java:122)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:428)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:420)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:268)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:249)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
at com.mycompany.app.App.main(App.java:24)
... 8 more
Caused by: java.io.IOException: Unable to open root Jar file 'file:/Users/subnara/Documents/Code/DevOps/MicroServices+Docker+Kubernetes/Sample/my-app/target/lib/spring-boot-actuator-autoconfigure-2.1.0.RELEASE.jar'
at org.springframework.boot.loader.jar.Handler.getRootJarFile(Handler.java:344)
at org.springframework.boot.loader.jar.Handler.getRootJarFileFromUrl(Handler.java:325)
at org.springframework.boot.loader.jar.Handler.openConnection(Handler.java:99)
at java.base/java.net.URL.openConnection(URL.java:1126)
at org.springframework.core.io.UrlResource.getInputStream(UrlResource.java:170)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:133)
at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:122)
at org.springframework.core.io.support.SpringFactoriesLoader.loadSpringFactories(SpringFactoriesLoader.java:139)
... 16 more
Caused by: java.lang.IllegalArgumentException: File /Users/subnara/Documents/Code/DevOps/MicroServices Docker Kubernetes/Sample/my-app/target/lib/spring-boot-actuator-autoconfigure-2.1.0.RELEASE.jar must exist
at org.springframework.boot.loader.data.RandomAccessDataFile$FileAccess.openIfNecessary(RandomAccessDataFile.java:234)
at org.springframework.boot.loader.data.RandomAccessDataFile$FileAccess.<init>(RandomAccessDataFile.java:216)
at org.springframework.boot.loader.data.RandomAccessDataFile$FileAccess.<init>(RandomAccessDataFile.java:206)
at org.springframework.boot.loader.data.RandomAccessDataFile.<init>(RandomAccessDataFile.java:49)
at org.springframework.boot.loader.jar.JarFile.<init>(JarFile.java:88)
at org.springframework.boot.loader.jar.Handler.getRootJarFile(Handler.java:338)
... 23 more
I am a noob in this area and any help is much appreciated.
TL;DR: Use the spring-boot-maven-plugin maven plugin and take a look at https://start.spring.io/.
When I try to reproduce the problem:
create a folder with the specified pom.xml and App.java file in it
run mvn install in the folder
run java -jar target/my-app-1.0-SNAPSHOT.jar
I don't run into the problem.
Since we are using the same code (assuming your question contains the same code as you were using), I wonder whether the problem might be related to your version of Maven and Java. I am using Maven 3.6.3 and java 11.0.11.
That being said, I noticed the build configuration in your pom is rather complex. It can be simplified significantly using spring-boot-maven-plugin.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Since your issue seems to be related to missing libraries and this spring-boot plugin also handles adding these libraries to the generated jar, switching to this plugin might also solve your problems.
Since you mention you just created the project, I would like to suggest you take a look at https://start.spring.io/. It's a too to bootstrap new spring boot projects. One advantage, and the reason I'd suggest it to you, is that it generates a project that should work. You don't have to figure out which plugins or dependencies to add, the generator does that for you.
I add the slick2d dependency to my pom.xml file but it highlights an error in red saying Could not find artifact javax.jnlp:jnlp-api:jar:5.0 at specified path at the top of the file where it says:
<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">
Even after running the test command in vscode it still gives a similar error: Could not resolve dependencies for project com.github.shia5347.universalelement:UniversalElement:jar:1.0-SNAPSHOT: Could not find artifact javax.jnlp:jnlp-api:jar:5.0 at specified path /usr/lib/jvm/java-8-openjdk/jre/lib/javaws.jar.
Here is my pom.xml in case needed:
<?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.shia5347.universalelement</groupId>
<artifactId>UniversalElement</artifactId>
<version>1.0-SNAPSHOT</version>
<name>UniversalElement</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slick2d/slick2d-core -->
<dependency>
<groupId>org.slick2d</groupId>
<artifactId>slick2d-core</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
I am using linux.
Since I dont like to be limited to Oracle JDK, I use the following:
<dependency>
<groupId>org.slick2d</groupId>
<artifactId>slick2d-core</artifactId>
<version>1.0.2</version>
<exclusions>
<exclusion>
<groupId>javax.jnlp</groupId>
<artifactId>jnlp-api</artifactId>
</exclusion>
</exclusions>
</dependency>
This will tell maven to skip the jnlp-api module.
It will also allow me to use AdoptOpenJDK with slick2d.
The easy way to resolve your issue is to use Oracle JDK.
From slick2d site at https://github.com/nguillaumin/slick2d-maven:
Slick 2D depends on javaws.jar which ships with the Oracle JDK (It's not available in the public Maven repositories). The pom.xml file references a local filesystem path to javaws.jar for that reason.
It won't work with OpenJDK for the reason above (Can be solved by providing javaws.jar separately). On some Linux distributions, you can install Netx and change the system path to point to netx.jar. For example on Ubuntu the package to install is icedtea-netx-common and the jar is in /usr/share/icedtea-web/netx.jar
See also this link Openjdk and Java webstart
I am trying to make my own Javac plugin but things aren't going so well already :( I am following this tutorial: https://www.baeldung.com/java-build-compiler-plugin and when I want to test the first basic plugin it can't find it.
When I enter the following command:
javac -cp ./target/classes/javacplugin/ -Xplugin:Getter ./src/main/java/javacPlugin/App.java
I get this error:
plug-in not found: Getter
How do I solve this issue?
This is my sourcetree:
This is my code for the plugin:
package javacplugin;
import com.sun.source.util.JavacTask;
import com.sun.source.util.Plugin;
import com.sun.tools.javac.api.BasicJavacTask;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Log;
/**
* JavacPlugin
*/
public class JavacPlugin implements Plugin {
#Override
public String getName() {
return "Getter";
}
#Override
public void init(JavacTask task, String... arg1) {
Context context = ((BasicJavacTask) task).getContext();
Log.instance(context).printRawLines(Log.WriterKind.NOTICE, "Hello from " + getName());
}
}
This is what's inside of com.sun.source.util.Plugin:
javacplugin.JavacPlugin
This is my 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>javacplugin</groupId>
<artifactId>javacplugin</artifactId>
<version>1</version>
<name>javacplugin</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<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>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.8.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<compilerArgs>
<arg>-Xplugin:Getter</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
You likely need your META-INF/ in the root directory, not in resources.
"To achieve this, we need to create a file named com.sun.source.util.Plugin with content which is our plugin’s fully qualified class name (com.baeldung.javac.SampleJavacPlugin) and place it in the META-INF/services directory."
From the referenced baeldung article
I have solved it:
javac -cp ./target/classes -Xplugin:Getter ./src/main/java/javacPlugin/App.java
Is the right command, without /javacplugin.
Actually i want to create a small "Hello WOrld"-like Example of a JavaSE-Application with Weld-SE, but it seems that there is a runtime error.
Here is my class:
package de.mycompany.weldapp;
import java.util.List;
import javax.enterprise.event.Observes;
import javax.inject.Singleton;
import org.jboss.weld.environment.se.bindings.Parameters;
import org.jboss.weld.environment.se.events.ContainerInitialized;
#Singleton
public class App
{
public void printHello(#Observes ContainerInitialized event, #Parameters List<String> parameters) {
System.out.println("Hello " + parameters.get(0));
}
}
An here is my 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>de.mycompany</groupId>
<artifactId>weldapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>weldapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-shaded</artifactId>
<version>3.0.2.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>de.mycompany.weldapp.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
I have installed Java JDK 1.8.0_151 and Maven 3.5.2.
The compile-process was successful, but when i execute the created jar-file in the target-directory, i will get the following error all the time:
A JNI error has occurred, plase check your installation and try again.
Exception in thread "main" java.lang.noClassDefFoundError: org/jboss/environment/se/events/ContainerInitialized
at java.lang.Class.getDeclaredMethods0(native Method)
...
Caused by : java.lang.ClassNotFoundException: org.jboss.weld.environment.se.events.ContainerInitialized
at java.net.URLCLassLoader.findClass(Unknown Source)
...
Is there anything that i did not mention or that i am doing wrong? Is there any dependency, that was not included?
I have installed Java, Maven and Eclipse again, but nothing helped.
Thanks a lot
Daniel
Since you're running in SE mode, you do not want to make any dependencies provided. So remove <scope>provided</scope> from your cdi-api dependency.
Edit: I reran your test, and realized what's happening for you. You're using the maven-jar-plugin to create your jAR. I'm assuming though you want to use the shade plugin to create an uber JAR. Here's an updated pom.xml you can use that seems to work (note that I also changed your main class to be the Weld start up class):
<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>de.mycompany</groupId>
<artifactId>weldapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>weldapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>3.0.2.Final</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.jboss.weld.environment.se.StartMain</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>