I've created a Discord bot in Eclipse and want to export it (Jar file). The problem is: I can't run it. Each time I try to start it it gives me this error: java.lang.NoClassDefFoundError: net/dv8tion/jda/core/entities/Game
I'm using "JRE System Library [JavaSE-1.8]" for my program.
This here is my pom.xml file
https://pastebin.com/bcs51jm9
After some research it seems like Eclipse doesn't implement the Maven Librarys into the Jar-file.
How can I fix this?
Exporting a jar with Eclipse from the wizard is generally the way when you don't have a build tool that allows to do it in a straight way.
You use Maven. So you have this tool.
What you need is configuring your pom.xml to enable the assembly plugin execution with the jar-with-dependencies descriptorRef.
It allows to specify the main class and to create a fat jar that contains all required dependencies at runtime according to the Maven dependencies declaration defined in your pom.
By executing mvn assembly:single you could so generate an additional jar : a jar-with-dependencies.
Related
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 just want to know how to include a jar file that has all dependent jars with it, as a dependency itself of an another project. I have tried export as runnable jar option and though it does work when I run the project as standalone, however I get noclassdeffound errors when I include the jar itself as a dependency for another project. To summarize suppose I have project A which depends upon some external jars a.dep1 and a.dep2 I include them in the jar by exporting the project A as a runnable jar file. Now I wish to use project A itself as a dependency in project B and for that purpose I include the jar of project A in my project B. But when trying to run I get the noclassdeffound errors. I don't want to use maven plugins. Is this possible?
for such cases you should be using maven
then you need to create a fat jar.
a fat jar will include all the dependencies it needs inside .
such a jar can be created using the assembly plugin you can see an example here:
assembly plugin
in general if you are using maven you do not have to do this as maven will bring all the dependencies your jar needs based on the pom file of your first jar.
A jar is just a collection of files; you have free rein on what you want to include in it via the command line. https://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html should teach you what you need to know
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 Eclipse Kepler for Java. Normally you can add internal/external .jars to a Java project in the build path located in the properties. Why, when I clone a git repo and import it into my projects, do I lose that ability? I don't understand. I kinda need to do that.
This is probably because the .gitignore has been configured to ignore .jar
Open the .gitignore file and remove the line *.jar, you should be able to add it.
======
As an aside - usually, for Java projects .jar files are not committed to repository (as they are large & it can slow down repository cloning), instead maven or gradle is used to configure dependencies. Example - http://www.mkyong.com/maven/how-to-create-a-java-project-with-maven/
Then when you want to work with eclipse just run mvn eclipse:eclipse to generate the necessary files. .gitgnore is usually set up to ignore *.class, *.jar, .project, .settings, .classpath
I found that if you open the run configurations and go to the Classpath tab that you can add internal/external .jars. The run configuration can be accessed by clicking Run > Run configurations. I added my .jar to the user entries. The bootstrap entry caused a null pointer.
When a project relies on libraries/modules, it's best to use a build tool for dependency management. JVM ecosystem is dominated with three build tools: Gradle, Maven and Ant.
How it works:
In a build script we declare dependencies of the project. This tells the build tool where to retrieve libraries/modules our project depends on. Dependencies are resolved at runtime; downloaded from a remote repository, retrieved from a local directory or if required another project to be built in a multi-project setting.
I have added to my pom.xml a section that specifies the mainClass and allows it to essentially create an executable jar. I have included a bunch of dependencies that maven manages as well. It compiles, and if I run the program with no options, it executes fine, displaying usage information. However, if I actually pass in the parameters, it fails and says NoClassDefFoundError: com/sas/isd/midasapi/ParticipantDetailExt, which is in a jar I included as an external jar. I am confused that it compiled and runs to show usage information, but it fails to find the class after as the ParticipantDetailExt is a class that is imported. Wouldn't it identify that it was not found during compiling? How do I get it so that my on jar with ParticipantDetailExt is seen when I run my exectutable jar? Is there a classpath thing or pom thing I need to do in addition to the adding jar as external jar?
I assume that you are running mvn clean package or mvn clean install to create your jar.By default the jar created by a maven project does not include dependencies in that jar.
Option 1# create a jar-with-dependencies, see: How can I create an executable JAR with dependencies using Maven?.
Option 2# If you are just looking to copy dependencies to a lib folder see: http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html