How to add an external library?

Hi everyone,

as you could expect from a title like this, I’m trying too add an external library for my plugin. To be exact: org.json.
However, I’m a total noob at Maven.

After some googling I found out I need to somehow include the jar in the classpath at runtime. However, adding this to the build path doesn’t help.

How do I have to do this?
Again, I’m a noob at Maven.
Feel free to ask for code.

Thanks in advance.

Well, I was running into an similar issue today. SO if you are using intellij and using Gradle, you edit the build.gradle and add the dependencies. If it’s a maven repository you add the repo under repo, and under dependencies add a compile line for it. then rerun build and refresh the project.

Thanks for the quick reply. I forgot to say I am using Eclipse and Maven (currently).

Here’s 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>io.github.runningchickendev.ecomarket</groupId>
  	<artifactId>EcoMarket</artifactId>
  	<version>0.2</version>
  
	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
	  		<plugin>
	    		<artifactId>maven-compiler-plugin</artifactId>
	    		<version>3.3</version>
	    		<configuration>
	      			<source>1.8</source>
	      			<target>1.8</target>
	    		</configuration>
	  		</plugin>
		</plugins>
	</build>
  
  	<repositories>
  	
  		<repository>
  			<id>sponge</id>
  			<url>http://repo.spongepowered.org/maven</url>
  		</repository>
  		
  		<repository>
  			<id>JSON</id>
  			<url>http://mvnrepository.com/artifact/org.json/json</url>
  		</repository>
  		
  	</repositories>
  
  	<dependencies>
  	
		<dependency>
    		<groupId>org.spongepowered</groupId>
        	<artifactId>spongeapi</artifactId>
        	<version>5.0.0</version>
        	<scope>provided</scope>
    	</dependency>
    	
    	<dependency>
    		<groupId>org.json</groupId>
    		<artifactId>json</artifactId>
    		<version>20160810</version>
    		<scope>compile</scope>
    	</dependency>
    	
	</dependencies>
	
</project>

I’m using ‘Maven install’.

However, it throws a ClassNotFoundException. Where does it go wrong?

errr, I haven’t used maven at all :confused: Everything I’ve done so far has been through gradle.
I’m guessing you might have to have maven redo your project structure. Unfortunately I don’t know where that is. There should be a refresh button somewhere. SOrry for any spelling errors I am not wearing my glasses atm. Makke sure the dependencies appear in your external libraries area, that’s usually how you make sure that the project has properly registered them.

You have multiple options
-define maven dependency with the compiled scope, then build project with maven command package. In this case library will be included in shaded jar - together with your plugin.

-load jar from external location
- if you put any jar into the mods folder forge (and only forge) will load it during startup.
- write a classloader/use net.minecraft.launchwrapper

also be aware that there are some exclusions

1 Like

Thanks for the help,

but

I’ve tried that, to no avail,

I have no idea how to use that or where to start.

Would it be a better idea to do it in gradle instead? I’ve heard good things about it.

They show up in Eclipse, but it still fails to be loaded :worried:

Basically, what happens is, that you’re getting the error because the library wasn’t compiled and included in the plugin JAR. This is quite an annoying problem. There are ways of including the dependencies, but you have to make sure to only include the org.json dependency and not the spongeapi.
From little experience, I found that this problem is a little easier to solve with Gradle than Maven, as I feel like the configuration of the packaging plugin is simpler with Gradle, and I don’t know whether I ever got it to work with Maven. Good luck, though.

Honestly I think gradle does a pretty great job of managing all the maven dependencies, i’d consider using it. I’d even consider switching to IntelliJ, it’s intellisense and code templating is so handy. Honestly from my experience when I first did my switch…It is so much more fluid than Eclipse or Netbeans, and it comes with gradle support out of the box, which as it turns out extremely handy and after learning a few things really handy.

Did I mention it is free (and the enterprise edition can be free if you work on open source projects I believe you have to meet some criteria)

This guide is pretty great and straight forward!
https://docs.spongepowered.org/master/en/plugin/workspace/idea.html

1 Like