3

I am trying to call a NetLogo model from my Java app. I am using right now the sample code:

public static void main(String[] argv) {

    HeadlessWorkspace workspace = HeadlessWorkspace.newInstance();
    try {
        workspace.open("models/Sample Models/Earth Science/Fire.nlogo");
        workspace.command("set density 62");
        workspace.command("random-seed 0");
        workspace.command("setup");
        workspace.command("repeat 50 [ go ]");
        System.out.println(workspace.report("burned-trees"));
        workspace.dispose();
    } catch (IOException | CompilerException | LogoException | InterruptedException ex) {
        ex.printStackTrace();
    }
}

I have added the dependency in my Netbeans project, but when I try to run it, I get:

------------------------------------------------------------------------
Building JAGO3 1.3
------------------------------------------------------------------------
The POM for org.nlogo:NetLogo:jar:5.2 is missing, no dependency information available

COMPILATION ERROR : 
-------------------------------------------------------------
com/digitaldust/jago3/NetLogoManager.java:[16,55] error: cannot access ScalaObject
1 error

this code is added in my POM:

<dependency>
    <groupId>org.nlogo</groupId>
    <artifactId>NetLogo</artifactId>
    <version>5.2</version>
    <scope>compile</scope>
    <type>jar</type>
</dependency>

It looks like there is no POM for NetLogo, even if it's added as a dependency... Anyway, being a Maven newbie, I don't understand if I am missing something... any help more than welcome.

EDIT

I have tried adding the NetLogo.jar from my NetLogo folder as a local dependency, using this snippet:

<dependency>
    <groupId>org.nlogo</groupId>
    <artifactId>NetLogo</artifactId>
    <version>5.2</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/NetLogo.jar</systemPath>
</dependency>

I am able to load the dependency from my current NetLogo installation, but I still get a (different) error:

--- maven-resources-plugin:2.5:resources (default-resources) @ JAGO3 ---
[debug] execute contextualize
Using 'UTF-8' encoding to copy filtered resources.
Copying 26 resources

--- maven-compiler-plugin:2.3.2:compile (default-compile) @ JAGO3 ---
Nothing to compile - all classes are up to date

--- exec-maven-plugin:1.2.1:exec (default-cli) @ JAGO3 ---
Exception in thread "main" java.lang.NoClassDefFoundError:      org/nlogo/api/CompilerException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2693)
at java.lang.Class.privateGetMethodRecursive(Class.java:3040)
at java.lang.Class.getMethod0(Class.java:3010)
at java.lang.Class.getMethod(Class.java:1776)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.nlogo.api.CompilerException
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more

EDIT2

I managed to import all jars in the lib folder manually with:

<dependency>
        <groupId>org.nlogo</groupId>
        <artifactId>netlogo</artifactId>
        <version>5.2</version>
        <scope>compile</scope>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.objectweb.asm</groupId>
        <artifactId>asm-all</artifactId>
        <version>3.3.1</version>
        <scope>compile</scope>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.apache.commons.codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.6</version>
        <scope>compile</scope>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>com.sun.gluegen</groupId>
        <artifactId>gluegen-rt</artifactId>
        <version>1.1.1</version>
        <scope>compile</scope>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents.httpclient</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.2</version>
        <scope>compile</scope>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents.httpcore</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.2</version>
        <scope>compile</scope>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents.httpmime</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.2</version>
        <scope>compile</scope>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.jhotdraw</groupId>
        <artifactId>jhotdraw</artifactId>
        <version>6.0b1</version>
        <scope>compile</scope>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>javax.media</groupId>
        <artifactId>jmf</artifactId>
        <version>2.1.1e</version>
    </dependency> 
    <dependency>
        <groupId>org.jogamp.jogl</groupId>
        <artifactId>jogl</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>
    <dependency>
        <groupId>mrj</groupId>
        <artifactId>mrjadapter</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.parboiled</groupId>
        <artifactId>parboiled-core</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.parboiled</groupId>
        <artifactId>parboiled-java</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.pegdown</groupId>
        <artifactId>pegdown</artifactId>
        <version>1.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.picocontainer</groupId>
        <artifactId>picocontainer</artifactId>
        <version>2.13.6</version>
    </dependency>
    <dependency>
        <groupId>org.devzendo</groupId>
        <artifactId>Quaqua</artifactId>
        <version>7.3.4</version>
    </dependency>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.jdesktop.layout</groupId>
        <artifactId>swing-layout</artifactId>
        <version>7.3.4</version>
    </dependency>  

The only issue I have now is that, having a project based on Spark, I already had a scala dependency higher than the NetLogo one... I just removed the Spark scala and Netlogo now works fine.

user299791
  • 2,021
  • 3
  • 31
  • 57
  • https://github.com/NetLogo/NetLogo/issues/256. but adding the JAR locally should work. but I don't use Maven, and it isn't obvious to me from the error message you're getting what's wrong. note that NetLogo.jar isn't self-contained though — it depends on all of the JARs in NetLogo's lib directory. – Seth Tisue Oct 16 '15 at 19:22
  • I don't get the reference to the discussion... do you have a maven repo at ccl? btw, I have Netlogo and the lib folder locally... but still something's missing, I guess... – user299791 Oct 16 '15 at 19:50
  • we don't, that's why the ticket remains open and you have to add the jar locally instead – Seth Tisue Oct 16 '15 at 19:56
  • I see... I managed to add the jar following http://stackoverflow.com/questions/17693040/adding-external-jar-to-maven-project-in-netbeans, but failed to load also its dependencies... – user299791 Oct 16 '15 at 20:08
  • Looks like you figured this out — it's OK on Stack Overflow to write an answer to your own question. – Seth Tisue Oct 17 '15 at 21:14
  • almost... my project contains Spark jars, and I am still battling with different scala versions... but I will answer my question – user299791 Oct 17 '15 at 22:51
  • NetLogo 5.2 requires Scala 2.9 on the classpath. Spark uses Scala 2.10, so if you need both Spark and NetLogo running in the same class loader, you'll be stuck; different Scala versions aren't binary compatible. It should be feasible to use a Scala 2.11 build of Spark, and then build NetLogo yourself off the `hexy` branch or the NetLogo-Headless repo, both of which use Scala 2.11. – Seth Tisue Oct 18 '15 at 08:11
  • By the way, you use a completely obsolete and unmaintained version of JOGL (1.1.1) whereas JOGL 2.3.2 was released recently. – gouessej Oct 19 '15 at 11:58
  • I have used the jars in NetLogo lib folder – user299791 Oct 19 '15 at 12:30

2 Answers2

2

NetLogo 6.0+ is now available at bintray but some work still needs to be done to download it properly via Maven. First you have to add the bintray repository to Maven - either as a remote repository (https://dl.bintray.com/netlogo/NetLogo-JVM) or directly to your ~/.m2/settings.xml file.

<?xml version="1.0" encoding="UTF-8" ?> <settings xsi:schemaLocation='http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd' xmlns='http://maven.apache.org/SETTINGS/1.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> <profiles> <profile> <repositories> <repository> <snapshots> <enabled>false</enabled> </snapshots> <id>bintray-netlogo-NetLogo-JVM</id> <name>bintray</name> <url>http://dl.bintray.com/netlogo/NetLogo-JVM</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <snapshots> <enabled>false</enabled> </snapshots> <id>bintray-netlogo-NetLogo-JVM</id> <name>bintray-plugins</name> <url>http://dl.bintray.com/netlogo/NetLogo-JVM</url> </pluginRepository> </pluginRepositories> <id>bintray</id> </profile> </profiles> <activeProfiles> <activeProfile>bintray</activeProfile> </activeProfiles> </settings>

Then add the following to your pom.xml to exclude a missing parser dependency that gets filled in via the scala parser combinator modules.

<dependency> <groupId>org.nlogo</groupId> <artifactId>netlogo</artifactId> <version>6.0.1</version> <exclusions> <exclusion> <groupId>org.nlogo</groupId> <artifactId>parser</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.picocontainer</groupId> <artifactId>picocontainer</artifactId> <version>2.13.6</version> </dependency> <dependency> <groupId>org.ow2.asm</groupId> <artifactId>asm-all</artifactId> <version>5.0.3</version> </dependency> <dependency> <groupId>org.scala-lang.modules</groupId> <artifactId>scala-parser-combinators_2.12</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>org.parboiled</groupId> <artifactId>parboiled_2.12</artifactId> <version>2.1.4</version> </dependency>

A Lee
  • 7,828
  • 4
  • 35
  • 49
  • i got this error in th pom.xml file 'Missing artifact org.jhotdraw:jhotdraw:jar:6.0b1' – Fray Oct 07 '19 at 10:42
0

I managed to import all jars in the lib folder manually with:

<dependency>
    <groupId>org.nlogo</groupId>
    <artifactId>netlogo</artifactId>
    <version>5.2</version>
    <scope>compile</scope>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>org.objectweb.asm</groupId>
    <artifactId>asm-all</artifactId>
    <version>3.3.1</version>
    <scope>compile</scope>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>org.apache.commons.codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.6</version>
    <scope>compile</scope>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>com.sun.gluegen</groupId>
    <artifactId>gluegen-rt</artifactId>
    <version>1.1.1</version>
    <scope>compile</scope>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents.httpclient</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.2</version>
    <scope>compile</scope>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents.httpcore</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.2</version>
    <scope>compile</scope>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents.httpmime</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.2</version>
    <scope>compile</scope>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>org.jhotdraw</groupId>
    <artifactId>jhotdraw</artifactId>
    <version>6.0b1</version>
    <scope>compile</scope>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>javax.media</groupId>
    <artifactId>jmf</artifactId>
    <version>2.1.1e</version>
</dependency> 
<dependency>
    <groupId>org.jogamp.jogl</groupId>
    <artifactId>jogl</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
</dependency>
<dependency>
    <groupId>mrj</groupId>
    <artifactId>mrjadapter</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>org.parboiled</groupId>
    <artifactId>parboiled-core</artifactId>
    <version>1.0.2</version>
</dependency>
<dependency>
    <groupId>org.parboiled</groupId>
    <artifactId>parboiled-java</artifactId>
    <version>1.0.2</version>
</dependency>
<dependency>
    <groupId>org.pegdown</groupId>
    <artifactId>pegdown</artifactId>
    <version>1.1.0</version>
</dependency>
<dependency>
    <groupId>org.picocontainer</groupId>
    <artifactId>picocontainer</artifactId>
    <version>2.13.6</version>
</dependency>
<dependency>
    <groupId>org.devzendo</groupId>
    <artifactId>Quaqua</artifactId>
    <version>7.3.4</version>
</dependency>
<dependency>
    <groupId>org.scala-lang</groupId>
    <artifactId>scala-library</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>org.jdesktop.layout</groupId>
    <artifactId>swing-layout</artifactId>
    <version>7.3.4</version>
</dependency>  

If you are on NetBeans, you have to right-click and "import artifacts manually" for each of the nodes created by the project POM. This should solve the issue with Maven, unless you have other scala imports that complains with the version used by NetLogo. In that case, see comment by @Seth Tisue above.

user299791
  • 2,021
  • 3
  • 31
  • 57