Running a java jar with eclipse swt dependencies with maven-dependency-plugin - java

I'm making an eclipse plugin that I also want to make it work command line. As normal the usual problem is encountering the eclipse dependencies in command-line:
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Layout
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:56)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Layout
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
Now I know there is an eclipse FAQ for this problem however here is a bit different. I'm using Maven for build with the maven-dependency-plugin to add jars I need from other project to this jar as dependencies:
<build>
<plugins>
<plugin>
<!-- Copy non-Eclipse plugins to target/dependency so that may be referenced
for runtime use. -->
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy-dependencies</id>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- seems the line below is needed otherwise every second run the
copy-dependencies fails , link to bug : https://bugs.eclipse.org/bugs/show_bug.cgi?id=393978 -->
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The dependencies in my Manifest file looks like this:
Rsrc-Class-Path: ./
lib/jcommon-1.0.23.jar
lib/jfreechart-1.0.19.jar
lib/jfreechart-1.0.19-swt.jar
lib/jfreechart-1.0.19-experimental.jar
lib/swt.jar
target/dependency/my.first.package.jar
target/dependency/my.second.package.jar
Class-Path: .
Rsrc-Main-Class: my.package.Main
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
Bundle-ClassPath: .,
lib/jcommon-1.0.23.jar,
lib/jfreechart-1.0.19-experimental.jar,
lib/jfreechart-1.0.19-swt.jar,
lib/jfreechart-1.0.19.jar,
lib/swt.jar
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.ui,
my.first.package;visibility:=reexport,
my.second.package;visibility:=reexport,
ca.odell.glazedlists,
org.eclipse.nebula.widgets.nattable.core,
org.eclipse.core.resources,
org.eclipse.swt
As you can see I added an swt.jar to a lib inside my project and am referencing it there as to have all my dependencies inside my jar.
Still I get the above error when I try to run it command line as if the swt.jar isn't there.
What am I missing?

If you want to make an Eclipse plugin command-line friendly, the best thing is to have the plugin also define an Eclipse application that you can then invoke from command-line: https://wiki.eclipse.org/FAQ_How_do_I_create_an_application%3F

Related

Hibernate config file not found

I am working on a java plugin that utilizes Hibernate JPA for my database interactions. I am using a Hibernate configuration file to accomplish this task.
When I export my project into a jar file and run it, I get the following error:
[23:23:12] [Server thread/ERROR]: Error occurred while enabling nationsatwar v1.1 (Is it up to date?)
java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
at net.mcnations.nationsatwar.PluginMain_Load.onEnable(PluginMain_Load.java:31) ~[?:?]
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:351) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
at org.bukkit.craftbukkit.v1_16_R1.CraftServer.enablePlugin(CraftServer.java:493) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
at org.bukkit.craftbukkit.v1_16_R1.CraftServer.enablePlugins(CraftServer.java:407) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
at net.minecraft.server.v1_16_R1.MinecraftServer.loadWorld(MinecraftServer.java:438) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
at net.minecraft.server.v1_16_R1.DedicatedServer.init(DedicatedServer.java:219) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
at net.minecraft.server.v1_16_R1.MinecraftServer.v(MinecraftServer.java:810) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
at net.minecraft.server.v1_16_R1.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
at java.lang.Thread.run(Thread.java:830) [?:?]
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
at java.net.URLClassLoader.findClass(URLClassLoader.java:436) ~[?:?]
at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:167) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:96) ~[spigot-1.16.1.jar:git-Spigot-0509002-6b95d59]
at java.lang.ClassLoader.loadClass(ClassLoader.java:588) ~[?:?]
at java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[?:?]
With the actual error being thrown on this line of code:
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml")
This immediately suggests my hibernate.cfg.xml file is not being exported in the project. I checked the project files in the export wizard, and I could not even find the configuration file in the window.
(Note the missing hibernate.cfg.xml file!!! (should be under the worldedit package))
Finally, in case its relevant, I am using the following dependency in my POM for Hibernate:
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.18.Final</version>
</dependency>
What am I doing wrong?
As stated Alex Funk in the comments:
java do not find org/hibernate/cfg/Configuration when trying to execute new Configuration
The problem is that the dependency is not packed into the jar file. Then, if you don't specify the .jar in the classpath and provide it at runtime, the JVM will not be able to create or access instances of classes of the dependency. In this case, the error indicates that the class org.hibernate.cfg.Configuration is not found. So you have two approaches:
Approach 1. Create a jar with the dependencies (Bigger .jar but recommended)
To do this you must override the maven assembly plugin and configure it the following way:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>jar-with-dependencies</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Then you can generate the jar via:
mvn install
Approach 2. Add the hibernate into the .jar classpath and provide it on runtime
I do not recommend this approach because it breaks one of the main advantages of plugins. The user will have to download the library and place it in a lib folder in its server. So the plug-in will require more installation steps by the end-user instead or just 'plugging in' the plugin in the server plugins folder.
The maven configuration will be:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.example.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
Then in the server, users will have to provide the library jar into a lib folder inside the plugins folder:
MyServer
|--- plugins
| |--- lib
| |--- hibernate-core-5.4.18.Final.jar
| |--- YourPlugin-1.0.0.jar
|--- spigot-1.16.1.jar
... other server files and folders
Then execute:
mvn install
Let me know if this solves your problem!

Maven - importing a class after adding it as a dependency

I'm trying to use(import) a class added by a dependency of my project.
I added the dependency gson in my pom.xml as follows:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
And I ran mvn install after that.
Now I can find the jar dependencies in C:\Users\%Name%\.m2\repository\com\google\code\gson\gson\2.8.5, so I assume it is correctly installed.
When I try to import the class Gson in Java, several problems occurs:
The path to import isn't com.google.code.gson but com.google.gson, I don't understand why.
When I compile with mvn package, everything runs smoothly. But if I try to run the code with java -cp .\target\testJSON-1.0-SNAPSHOT.jar json.App I get the following error telling me the Gson class isn't found :
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/Gson
at json.App.main(App.java:13)
Caused by: java.lang.ClassNotFoundException: com.google.gson.Gson
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
Now from what I understood by looking for solutions:
While maven know the dependencies of the project, java won't. And I need to manually add the jar to the classpath.
So my questions are:
If I bother to tell maven I need gson to run my project, why do I need to manually include all dependencies to run it ? Is there a way to automate this thing in an easy and intuitive manner ?
When I look for ways to add multiples jars to the classpath, the only answers I find assume that all the jars are in the same folder, hence proposing to do java -cp folder/*.jar. The jar generated by Maven is located in the target subfolder of my project, while the gson jar is located in the .m2 folder. How to add multiple jars (located in different locations) to the classpath ?
Thank you,
The path to import isn't com.google.code.gson
Google Code got shut down in 2016, they have changed their group IDs.
the Gson class isn't found
The GSON JAR isn't on the classpath.
If I bother to tell maven I need gson to run my project, why do I need
to manually include all dependencies to run it ? Is there a way to
automate this thing in an easy and intuitive manner ?
Maven is a build tool, and it has built your app successfully. Java requires you to include the JARs on the classpath. This not a deficiency of Maven. If you want to bundle all the dependencies together into one JAR that "just runs" then create a fat JAR.
How to add multiple jars (located in different locations) to the
classpath ?
If you create a fat JAR, you won't have to, but you can separate classpath entries like so (OS-specific):
java -cp folder/*.jar;another/folder/*.jar
You may want to include the dependencies in when creating a jar with maven. You can use maven-assembly plugin,
<build>
<plugins>
<!-- any other plugins -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>

JDK com.sun.tools not found after Maven install

My Problem (with MAVEN) just exist on Windows (not Linux) and not Inside the IDE (Eclipse)
Class:
...
System.out.println("Location of Tools --> " + VirtualMachine.class.getProtectionDomain().getCodeSource().getLocation());
...
Exception:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/attach/VirtualMachine
at my.package.bootstrap(Controller.java:37)
at my.package.main(Main.java:35)
Caused by: java.lang.ClassNotFoundException: com.sun.tools.attach.VirtualMachine
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
My Environment:
Windows: Windows 10 (Here are the Problems!!!)
Linux: Xubuntu 16.04 (Works fine)
Java: 8 oracle (not openjdk!)
Maven (**using the maven-assembly-plugin for installing**)
My Project Architecture:
My Project which using the com.sun.tools is a library: lib.jar
which gets used by Main Project: Main.jar.
I'm using maven to compile all of that into Main.jar
What works:
Linux Eclipse IDE: found
Linux - Shell: java -jar myjar.jar: found
Windows Eclipse IDE: found
--> Windows - Bash: /java_jdk_path/java -jar myjar.jar: _NOT_FOUND_
My not-working-solutions to make it find the lib on windows in main.jar:
set %JAVA_HOME% ... to jdk
set %JRE_HOME% ... to jre
I found a lot of workarounds like i.e. maven profiles and so on, I tested all of them but nothing worked. One time
I got it worked, when I copyed the com.sun.tools via maven plugin (mvn install...) into the JAR. Then it worked.
But I need it to find the com.sun.tools dynamically - depending on the operating system linux and windows.
Any suggestions for my next steps? I don't know what I could do that it works.
I always get this exception when using this tools.jar on windows after executing it with ...java -jar Main.jar
Goal: I want to start Main.jar (java -jar Main.jar) on any folder I want.
UPDATE:
I added a sample Project, which fails for me exactly with this error.
Just create a new Maven Project and add this to Main.java
and to POM. Install it with: mvn install and it fails.
Use the eclipse jar builder and it works!??? WTF Why?
Code:
...
System.out.println("Location of Tools --> " + VirtualMachine.class.getProtectionDomain().getCodeSource().getLocation());
...
POM:
<properties>
<java.version>1.8</java.version>
<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</version>
<scope>system</scope>
<systemPath>${env.JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<outputDirectory>C:\Output\</outputDirectory>
<archive>
<manifest>
<mainClass>com.test.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I know its not the solution for the current problem, but I am using a workaround and I can recommend it to any other. I am using https://github.com/oshi/oshi now, because it has all the features what I need and wanted to get with that com.sun.tools.
But Anyway it would be nice to hear if anyone has a working solution for this maven install (maybe maven {java.home} bug?) problem.

Having trouble getting Joda Time to work with my bukkit plugin

I am attempting to use Joda-Time with my bukkit plugin, however I am running into ClassNotFoundException and a NoClassDefFoundError.
I am using eclipse and have added it to my Build Path, and modified my Class-Path variable in my MANIFEST in the jar (and then adding the jar to a lib folder outside of my jar file), as well as tried to add the joda-time jar files in my jar file.
So make myself a bit more clear... The ClassNotFoundException shows up, when I add the files manually to my jar the NoClassDefFoundError shows up.
I've tried converting my project to maven (via eclipse) and added joda-time as a dependency to the pom.xml, but I cant seem to get that to work either (Maybe I'm doing something wrong).
If you need anymore information please ask. I'm not sure what else to include.
Thanks for any help you can give me.
java.lang.NoClassDefFoundError: org/joda/time/ReadableInstant
at com.**********.plugin.UMSBase.registerEvents(UMSBase.java:46)
at com.***********.plugin.UMSBase.onEnable(UMSBase.java:22)
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
at org.bukkit.craftbukkit.v1_5_R2.CraftServer.loadPlugin(CraftServer.java:282)
at org.bukkit.craftbukkit.v1_5_R2.CraftServer.enablePlugins(CraftServer.java:264)
at net.minecraft.server.v1_5_R2.MinecraftServer.j(MinecraftServer.java:303)
at net.minecraft.server.v1_5_R2.MinecraftServer.e(MinecraftServer.java:282)
at net.minecraft.server.v1_5_R2.MinecraftServer.a(MinecraftServer.java:242)
at net.minecraft.server.v1_5_R2.DedicatedServer.init(DedicatedServer.java:150)
at net.minecraft.server.v1_5_R2.MinecraftServer.run(MinecraftServer.java:381)
at net.minecraft.server.v1_5_R2.ThreadServerApplication.run(SourceFile:573)
Caused by: java.lang.ClassNotFoundException: org.joda.time.ReadableInstant
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at org.bukkit.plugin.java.PluginClassLoader.findClass0(PluginClassLoader.java:80)
at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:53)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 13 more
Did you put Joda Time's jar file in the same folder as craftbukkit.jar.
I'm not sure if this will fix the problem.
Just add to build plugins maven-shade-plugin, which inserts Joda Time to plugin jar, so the classes will be found at runtime.
Into your pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>jar-with-dependencies</shadedClassifierName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then use name-with-dependencies.jar as your plugin file.

Maven local dependency not resolving sub-dependencies

I'm having problems resolving sub-dependencies from a third-party package. I'm a bit of a Maven beginner. Basically it goes like this:
git clone git://github.com/unidata/thredds.git
cd thredds
mvn install
Everything works great and stuff is installed into ~/.m2. Now, I wrote my own code that uses the package that I just installed:
<?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>
<packaging>jar</packaging>
<version>0.1</version>
<name>Test Package</name>
<dependencies>
<dependency>
<groupId>edu.ucar</groupId>
<artifactId>netcdf</artifactId>
<version>4.3.8-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Ok, this also works great when I run mvn package. The netcdf artifact is found. Now, when I try to run my code, I start down a long ClassNotFoundException path, having to keep adding all of netcdf's dependencies to my classpath.
Am I doing something wrong, or should the thredds package and all its dependencies automatically be picked up?
edit: the thredds package has many sub-modules one of which is netcdf. My code only depends on the netcdf jar.
edit: the snapshot version is installed
$ ls -l ~/.m2/repository/edu/ucar/netcdf/4.3.8-SNAPSHOT/
total 4272
-rw-rw-r-- 1 nwatkins nwatkins 700 2012-03-29 23:23 maven-metadata-local.xml
-rw-rw-r-- 1 nwatkins nwatkins 182 2012-03-29 23:23 _maven.repositories
-rw-rw-r-- 1 nwatkins nwatkins 4357494 2012-03-29 23:23 netcdf-4.3.8-SNAPSHOT.jar
-rw-rw-r-- 1 nwatkins nwatkins 7840 2012-03-29 22:28 netcdf-4.3.8-SNAPSHOT.pom
edit: to run the code which is in a single file Test.java
$ java -cp target/test-0.1.jar Test
edit: first error message
$ java -cp target/test-0.1.jar Test
Exception in thread "main" java.lang.NoClassDefFoundError: ucar/ma2/InvalidRangeException
Caused by: java.lang.ClassNotFoundException: ucar.ma2.InvalidRangeException
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
edit: then I just try to manually add the dependency jar to the class path
$ java -cp ../thredds/cdm/target/netcdf-4.3.8-SNAPSHOT.jar:target/test-0.1.jar Test
xception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at ucar.nc2.NetcdfFile.<clinit>(NetcdfFile.java:97)
at Test.main(Test.java:37)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
... 2 more
The slf4j package is also in ~/.m2. I stopped trying to add things to the classpath after this as it seemed like the wrong approach.
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Two solutions here (the cause of the problem has been explained in several answers):
1.Use maven exec plugin to launch your app:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<classpathScope>runtime</classpathScope>
<executable>java</executable>
<commandlineArgs>-classpath %classpath Test</commandlineArgs>
</configuration>
</plugin>
2.You can also package your jar with all its dependencies, using maven assembly plugin. To do that, you need to add the following to your plugins of your build:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>build-package</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Then you can use the output jar created by maven assembly in your classpath arg
It sounds like you are trying to run your project from the target folder, and it is not seeing the transitive dependencies. You will need to use the maven dependencies plugin to have those dependencies copied over along with your jar file.
See also:
http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html
You can then define the dependencies on these jars either manually, or via a generated manifest file:
http://maven.apache.org/shared/maven-archiver/index.html
The maven project that you've created only packages your classes into test-0.1.jar.
If you use a simple java command to run your application, you MUST provide all other jars that your project depends on to the -classpath ( or -cp ) switch ( or through CLASSPATH environment variable ).
There are alternative packaging configurations that can copy all required dependencies into a single jar, if that is what you want.
Here is the example, which is a verbatim copy from here -> http://www.sonatype.com/books/mvnref-book/reference/assemblies-sect-basics.html. I recommend that you read that whole chapter ( heck the whole book ) for better understanding.
<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>org.sonatype.mavenbook.assemblies</groupId>
<artifactId>executable-jar</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Assemblies Executable Jar Example</name>
<url>http://sonatype.com/book</url>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-2</version>
<executions>
<execution>
<id>create-executable-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>
jar-with-dependencies
</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.sonatype.mavenbook.App</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
If you want to have all the dependencies in classpath, when running your jar, you can use the maven-exec-plugin to run the program from withing maven, and have all the dependencies automatically added to your classpath.
You are missing the dependency for:
org/slf4j/LoggerFactory
which means in other words you need to add a dependency to your project:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
Assuming you are using log4j ?
And furthermore based on the missing class: ucar.ma2.InvalidRangeException it looks you need an other supplemental dependency except (May be take a look into the documentation if exists).
<dependency>
<groupId>edu.ucar</groupId>
<artifactId>netcdf</artifactId>
<version>4.3.8-SNAPSHOT</version>
</dependency>

Categories