I am having problems importing classes from a JAR library into my project. Please see the screenshot.
I have tried several things both in Eclipse and IntelliJ, both adding directly and adding through Maven. None of this helps, I still get a red underline.
In IntelliJ I tried:
Project Structure - Modules - Dependencies - Add jar.
Tried creating a lib folder, add it as a library and place the jar inside and then setup as dependency.
Adding through maven pom.xml with direct path to the jar.
In Eclipse I tried:
Java Build Path and adding it to my build path.
Maven - Update Project.
Here is my pom.xml to get the local jar.
<dependency>
<groupId>uk.co.pervasive_intelligence.simulator</groupId>
<artifactId>protocol</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>C:\Users\Vas-DELL\Desktop\simulator-1.2.2.jar</systemPath>
</dependency>
Strangely, I am able to see the jar and the classes inside the jar (screenshot). But still can not import them. Let me know please if there is anything else I can provide.
Create a lib/ dir in the root of your project folder. Put your Jar there. Add this to your pom.xml:
<dependency>
<groupId>uk.co.pervasive_intelligence.simulator</groupId>
<artifactId>protocol</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/simulator-1.2.2.jar</systemPath>
</dependency>
Do not use \ as path separator (even though you're using windows)
Run mvn clean package from the command line
You could also try installing the dependecy manually in your local repo:
mvn install:install-file -Dfile=simulator-1.2.2.jar -DgroupId=uk.co.pervasive_intelligence.simulator -DartifactId=protocol -Dversion=1.0 -Dpackaging=jar
Then add this to your pom:
<dependency>
<groupId>uk.co.pervasive_intelligence.simulator</groupId>
<artifactId>protocol</artifactId>
<version>1.0</version>
</dependency>
EDIT:
The jar file does not have the structure of a standard java library. In order to use that jar as a library, the packages of your classes should exist as folders in the base (or root directory) of your jar file. For example:
/META-INF
/MANIFEST.MF
/uk
/co
/pervasive_intelligence
/simulator
/BaseComponent.class
/SimulatorApplication.class
/SimulatorException.class
....
Being a library jar file then the contents of the MANIFEST.MF can be as simple as
Manifest-Version: 1.0
Hope this helps
Class files in co.uk... package is inside BOOT-INF/classes packed inside simulator jar, and cannot be accessed directly because it not part of jar's default classpath (".").
To understand better about jar classpath please check What's the default classpath when not specifying classpath?
simulator jar need to be packaged with classpath entry to add BOOT-INF/classes/... in classpath to allow access to classes under BOOT-INF/classes.
For example to allow access to classes from package uk.co.pervasive_intelligence.simulator.Component with maven this can be done as
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Class-Path>BOOT-NF/classes/uk/co/pervasive_intelligence/simulator/Component</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
If You Use SpringBoot and InteliJ Idea, when you have excute mvn clean package command, you should use packagename.jar.original in maven pom for The jar file have the structure of a standard java library.
/META-INF
/MANIFEST.MF
/uk
/co
/pervasive_intelligence
/simulator
/BaseComponent.class
/SimulatorApplication.class
/SimulatorException.class
....
Related
I would like to add a dependency to my maven project so that Maven can resolve the dependency, but not add it to the classpath. E.g.
<dependency>
<groupId>com.example</groupId>
<artifactId>example-artifact</artifactId>
<type>bin</type>
<classifier>jar-with-dependencies</classifier>
<version>1.0</version>
</dependency>
In this particular case, the dependency is a FAT executable jar file, that I want to run from my Mojo. I don't want the contents of that jar file to pollute my mojo's classpath. I just want it treated as a binary file, so that I can look it up inside my mojo (via project.getArtifacts(), and then execute it as CLI.
I've tried both "zip" and "bin" packaging types, but it always seems to end up on the classpath.
I could bundle the zip inside another jar dependency and then extract/run it at runtime, but I would prefer to just figure out how to resolve it but exclude it from the classpath.
Any ideas?
After reading Spring documentation and some other articles on web, I am still confused what is the difference between Spring Boot Maven plugin's spring-boot:repackage and a regular mvn package.
I've thought that mvn package creates a jar with all dependencies included, so what is really the reason to use the plugin by Spring?
The maven package goal and the spring-boot:repackage goal are different in nature. The spring-boot repackage goal is mainly intended to make a JAR or WAR executable from the command line itself using java -jar *.jar while the maven package goal take the compiled code and package it in its distributable format, such as a JAR.It is the spring-boot repackage goal that repackages the JAR produced by maven to specify the main class and make it executable using an embedded container.
Maven Package
The first, and most common way, to set the packaging for your project via the equally named POM element . Some of the
valid packaging values are jar, war, ear and pom. If no packaging
value has been specified, it will default to jar.
When a package is defined,each packaging contains a list of goals to bind to a particular phase ,the jar packaging will bind the
following goals to build phases of the default lifecycle :
process-resources,compile,process-test-resources,test-compile,test,package,install,deploy.
Spring-boot:repackage
Plugin to be included is :
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.4.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The configuration repackages a jar or war that is built during the package phase of the Maven lifecycle.
So,Once spring-boot-maven-plugin has been included in your pom.xml, it automatically tries to rewrite archives to make them executable by using the spring-boot:repackage goal. You should configure your project to build a jar or war (as appropriate) by using the usual packaging element.
Reference : https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html
Spring repackage a jar or war that is built during the package phase of the Maven lifecycle. The following example shows both the repackaged jar, as well as the original jar, in the target directory:
$ mvn package
$ ls target/*.jar
target/myproject-1.0.0.jar target/myproject-1.0.0.jar.original
If you don’t include the configuration, you can run the plugin on its own (but only if the package goal is used as well). For example:
$ mvn package spring-boot:repackage
$ ls target/*.jar
target/myproject-1.0.0.jar target/myproject-1.0.0.jar.original
See more details from spring website using the link
mvn package creates a jar or war.
The spring boot plugin takes that jar or war and repackages them to make them executable from the command line (i.e. no app server needed).
From the plugin docs:
"Repackages existing JAR and WAR archives so that they can be executed from the command line using java -jar."
I can't compile the files directly. I use mvn package.
I can't run the files directly. I use storm (Apache).
I don't know much about Maven.
I tried to just put the .jar in the same folder as the code and use import com.path.of.jar. It did compile, but when I tried to run, gave a NoClassDefFoundError.
Try this way to add dependencies directly, like this:
<dependency>
<groupId>sample</groupId>
<artifactId>com.sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/yourJar.jar</systemPath>
</dependency>
When you work on a maven based project, you manage dependencies through the pom.xml file at the root of the project. POM stands for Project Object Model and contains information about the project and configuration details used by Maven to build the project (Introduction to the POM).
A maven project produces an artifact uniquely identified by its coordinates: The <groupId>, <artifactId> and <version> that you normally find at the top of your pom.xml file. Once an artifact is published to a repository other maven projects can depend on it.
If you look at the content of your POM file you should see a <dependencies> element containing all dependencies that your project needs. If you want to import classes from a jar in your code you will need to find the maven coordinates of this jar (for example on search.maven.org or mvnrepository.com).
Once you have the coordinates add a corresponding dependency section. It should look like this:
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
Next time you run mvn package, the jar will be downloaded, used during compilation and packaged with your artifact.
And if you would like to get a good understanding of maven the following free book is excellent: Maven: The Complete Reference
I wrote a program where various java library's used in the program. I made a jar file of my program using netbeans. Now if I use that jar on any other location, then I will have to manually include all the jar libraries.
Is there any way so that all the dependent libraries should get build with my program's jar to build a single jar instead of so many jar files ?
I know there are programs which converts jar to exe, they also do the same thing but I want to get the last file into jar format, so that it could also run on Linux.
Thanks
You can use use One-Jar
One-JAR provides custom classloader that knows how to load classes and resources from a jars inside an archive, instead of from jars in the filesystem. It discovers dependency jar files based on the internal structure of the archive, there is no custom code required to do this. One-JAR archives can be constructed using Ant or Maven2. Your application can be run using java -jar my-app.jar
Using Maven: you need to update the plugins section pom.xml:
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
And update pluginRepositories section in pom.xml
<pluginRepository>
<id>onejar-maven-plugin.googlecode.com</id>
<url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>
When you will execute the mvn package you will get yourappname-one-jar.jar and you can run it java -jar yourappname-one-jar.jar
Yes it can be done. Since you are using Netbeans, this article may help you.
If you are using maven, this maven-shade-plugin is what you are looking for: https://maven.apache.org/plugins/maven-shade-plugin/
Is there a way to download dependencies from a pom.xml file to a specified folder in java? I'm able to run maven command from java and I got download messages, but I don't know where maven stores these libraries? How can I download these dependencies to a specific folder?
Take a look at maven's dependency plugin, specifically the
copy-dependencies goal. The usage section describes how to do exactly what you want.
To do it from the command line just do:
$ mvn dependency:copy-dependencies -DoutputDirectory=OUTPUT_DIR
Add this to exclude the transitive or inner dependencies:
-DexcludeTransitive=true
As explained here, you can use maven-dependency-plugin:get for this.
For example, if you want to download org.apache.hive:hive-common:2.1.1 in your local folder, execute this:
mvn dependency:get -Ddest=./ -Dartifact=org.apache.hive:hive-common:2.1.1
If you want to download the latest 3.0.0-SNAPSHOT:tar.gz version of com.orientechnologies:orientdb-community-gremlin from https://oss.sonatype.org/content/repositories/snapshots snapshots repository, execute this:
mvn dependency:get -Ddest=./ -DremoteRepositories=sonatype-nexus-snapshots::::https://oss.sonatype.org/content/repositories/snapshots -Dartifact=com.orientechnologies:orientdb-community-gremlin:3.0.0-SNAPSHOT:tar.gz
Add something similar to the following to pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<outputDirectory>
${project.build.directory}
</outputDirectory>
</configuration>
</plugin>
Then run mvn clean dependency:copy-dependencies to perform the copy.
Combine this with the assembly plugin and you can package everything into a self contained archive for distribution.
Maven stores all of these in it's local Maven2 repository. By default, it will store them in your user home directory under a directory called repository.
You can use the maven-dependency-plugin's goal called copy to take all of your project's dependencies and put them in a folder.
http://maven.apache.org/plugins/maven-dependency-plugin/copy-mojo.html
Go to this site: http://jar-download.com/online-maven-download-tool.php
Insert the Maven dependencies XML
Download the jar files as a ZIP.