I have a simple maven project where I have a simple class analyzing File encoding. I am using the juniversalchardet library from Google juniversalchardet library . In the pom.xml I added the dependency from link above. When I build the project with mvn clean install, everything looks fine. In the local repository I see tha universalchardet.jar file also. The class file in my project is also compiled without errors. But when I run my project .JAR i get an error like this:
Exception in thread "main" java.lang.NoClassDefFoundError: org/mozilla/universalchardet/UniversalDetector
Here is my dependency:
<dependency>
<groupId>com.googlecode.juniversalchardet</groupId>
<artifactId>juniversalchardet</artifactId>
<version>1.0.3</version>
</dependency>
I am not very familiar with Java - what I am doing wrong?
You can try this https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
where you mention your dependency jar in your manifest file : Class-Path: juniversalchardet.jar
Or
Consider making your jar as uber jar, refer: What is an uber jar?
Related
When I look at following the jar located at following coordinate on maven central:
<dependency>
<groupId>org.eclipse.pde</groupId>
<artifactId>org.eclipse.pde.build</artifactId>
<version>3.11.0</version>
</dependency>
I see a wired content. There is no *.class files inside.
Only the META-INF/MANIFEST.MF seems to be relevant and correct.
Corresponding to the source:
https://git.eclipse.org/c/pde/eclipse.pde.build.git/tree/org.eclipse.pde.build/META-INF/MANIFEST.MF
When I try to use the jar referenced by its maven central coordinates in my code, the Gradle build fails with an expected:
error: cannot access org.eclipse.pde.internal.build.IBuildPropertiesConstants
Which make sense because the class is located in pdebuild.jar inside the org.eclipse.pde:org.eclipse.pde.build:3.11.0 jar.
I guess this line in the MANIFEST telling the Eclipse compiler to look at the jar inside the jar as well:
Bundle-ClassPath: pdebuild.jar
If I put the pdebuild.jar (manually extracted from the jar fetched on maven central) on the classpath:
Then I am able to compile.
How to make Gradle aware of this Bundle-ClassPath OSGi directive?
I found this related question: Gradle: Properly build classpath from OSGI dependency's MANIFEST.MF
From the answer, this doesn't seems to be possible
How can I add an external library to a project in IntelliJ IDEA so that when I build an artifact it still has access to the classes in the library?
I have created a new Jar artifact from Project Structure, then added the external JAR to the Libraries, then checked it in the Modules List, and finally added it to the Output for the Artifact. None of these work. When I build and try running my application, it throws an error:
Exception in thread "main" java.lang.NoClassDefFoundError: <path of the class trying to use>
What am I missing, or am I doing this completely wrong?
You have 2 options here:
extract the dependency into the artifact jar so that the app is the single executable jar with all the dependencies
link the dependent jars via the Manifest.MF and copy them near the application main jar
I've prepared a sample project that demonstrates both approaches: HelloWithDependencies.zip.
The artifacts are produced into out\single and out\linked directories.
Relevant configurations:
If you are using maven to build your application then this is not the correct way to add external library. You should either
Do an install of your library like below mvn install:install-file -Dfile=myJar.jar -DgroupId=com.yourproject -DartifactId=yourproject -Dversion={version} -Dpackaging=jar.
Use system path like explained here.
Option 1 is prefered since you don't have to keep jar in your project.
I have a local Jar file that I want to add as a dependency for a Maven project that I am working on.
That is easy to do as this answer states.
The problem is that this Jar file requires a foo.classifier file to work properly.
This Jar file is meant to be run by command line (java -jar bar.jar {argument}). The Jar and the classifier files are in the same directory, so everything works just fine.
However, from my project, I want to call the Jar file using its methods and not running a process.
I added the Jar file as a dependency and managed to call the main method that receives the argument, however, it doesn't work properly because of the foo.classifier the file is not set as a dependency for that Jar file to work.
Does anyone know how can I set it as a dependency of that Jar file?
Put foo.classifier files in exact folder in .m2\repository folder where your local Jar file that you added as a dependency is present as dependency jar will be referred from local repository
You can put your jar to libs directory,and add below code into your pom.xml file:
<dependency>
<groupId>htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.21-OSGi</version>``
<scope>system</scope>
<systemPath>the full path of the jar</systemPath>
</dependency>
Add the jar to maven repository with the follows command line, specify a group id, artifact id and a version
mvn install:install-file -Dfile="my-local.jar" -DgroupId="my.groupid" -DartifactId="name" -Dversion="1.0.0" -Dpackaging=jar
and then add as dependency to your pom
<dependency>
<groupId>my.groupid</groupId>
<artifactId>name</artifactId>
<version>1.0.0</version>
</dependency>
How can I add an external library to a project in IntelliJ IDEA so that when I build an artifact it still has access to the classes in the library?
I have created a new Jar artifact from Project Structure, then added the external JAR to the Libraries, then checked it in the Modules List, and finally added it to the Output for the Artifact. None of these work. When I build and try running my application, it throws an error:
Exception in thread "main" java.lang.NoClassDefFoundError: <path of the class trying to use>
What am I missing, or am I doing this completely wrong?
You have 2 options here:
extract the dependency into the artifact jar so that the app is the single executable jar with all the dependencies
link the dependent jars via the Manifest.MF and copy them near the application main jar
I've prepared a sample project that demonstrates both approaches: HelloWithDependencies.zip.
The artifacts are produced into out\single and out\linked directories.
Relevant configurations:
If you are using maven to build your application then this is not the correct way to add external library. You should either
Do an install of your library like below mvn install:install-file -Dfile=myJar.jar -DgroupId=com.yourproject -DartifactId=yourproject -Dversion={version} -Dpackaging=jar.
Use system path like explained here.
Option 1 is prefered since you don't have to keep jar in your project.
I am using maven to build my java project.
I have a library jar named my.jar used as my project's dependency. However, it is not available in the remote central repository. So, I would like to put the jar file under my project. So, I created a folder named my_repo/ under MyProject.
The directory structure of MyProject looks like this:
MyProject/
my_repo/
my.jar
pom.xml
But I have no idea how could I define my pom to find this particular dependency under MyProject/my_repo/my.jar ?
Could someone please help me for my scenario? By the way, I have also some other dependencies defined in my pom.xml, they are available in the remote central repo.
Using the system scope. ${basedir} is the directory of your pom.
<dependency>
<artifactId>..</artifactId>
<groupId>..</groupId>
<scope>system</scope>
<systemPath>${basedir}/my_repo/my.jar</systemPath>
</dependency>
The recommended way is usually to use a Maven Repository Manager (e.g. Nexus) and then deploy your library to this Maven Repository Manager.
See here for how to set up the settings.xml file:
http://books.sonatype.com/nexus-book/reference/maven-sect-single-group.html
How to create maven dependency for your local jars..
1)Create a maven project of the program of which you have to create the jar.
2)Add required groupid and artifact id and version no.
3)Then after writing the java files export it as a jar.
4)Now open other project in which you want to add local jar dependency.Go into its pom.xml and add dependency with the group id,artifact id and version which u had entered in the jar project.
5)Maven clean and install.Now that jar file will be available in your local repository. :)