How to Create a Java Library w/ SDK v3.2

I selected New ProjectJavaJava Class Library to create a new library. Then I created a class extending BaseAppState (as a test). The problem is, it does not recognize com.jme3. How do I create a library that has com.jme3?

Add JME core as a dependency.

Or since you are using the SDK, create new Basic game template and you are all set.

Or… better yet, upgrade to SDK 3.3 and create new Gradle game template. Join the future, will save you some time and nerves later on.

1 Like

He wants to make a library not a game. My recollection of the game template is that it adds a bunch of other stuff and it makes it ill suited for treating as the dependency in another project.

@codex just a suggestion, but you might be very much interested in Gradle where you can even have “multiproject” project.

1 Like

Yep, the ANT java class library option in jme3-SDK is a feature of netbeans not jme added, it creates a pure java class project with nothing, but an empty package :

image

To create a class library,

For jme3 SDK(ANT), creating a new game template(as mentioned above) → then add what you want(from classes, code, jars) → build jar file → use it in another project, but at that point, you might want to check the jar dependency of jme3-core.jar, since com.jme3 isnot found at this location, add it manually if it’s not added by the IDE, under the libraries drop down menu :

NB : the java ANT library is nothing but a blank java project, so don’t bother yourself too much.

For gradle based projects :

  • Create a new folder
  • open cmd/terminal
  • type : cd FOLDERDIR
  • type gradle init
  • select option 3
  • select option 3
  • select option 1
  • ENTER

like that :

┌─[[email protected]]─[~/GradleProjects/javaLibraryExample]
└──╼ $gradle init

Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 3

Select implementation language:
  1: C++
  2: Groovy
  3: Java
  4: Kotlin
  5: Scala
  6: Swift
Enter selection (default: Java) [1..6] 3

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2] 1

Select test framework:
  1: JUnit 4
  2: TestNG
  3: Spock
  4: JUnit Jupiter
Enter selection (default: JUnit 4) [1..4] 

Project name (default: javaLibraryExample): 
Source package (default: javaLibraryExample): 

> Task :init
Get more help with your project: https://docs.gradle.org/6.7/samples/sample_building_java_libraries.html

BUILD SUCCESSFUL in 16s
2 actionable tasks: 2 executed

then, you will get a project with some files & gradle :

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java library project to get you started.
 * For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
 * User Manual available at https://docs.gradle.org/6.7/userguide/building_java_projects.html
 */

plugins {
    // Apply the java-library plugin for API and implementation separation.
    id 'java-library'
}

repositories {
    // Use JCenter for resolving dependencies.
    jcenter()
}

dependencies {
    // Use JUnit test framework.
    testImplementation 'junit:junit:4.13'

    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:29.0-jre'
}

add mavenCentral() & jme dependencies & your are ready to rock it.

https://docs.gradle.org/6.7/userguide/java_library_plugin.html#java_library_plugin

1 Like

Yes, but if the Java library needs

extending BaseAppState (as a test)

Maybe all that stuff is needed. Just saying.

1 Like

Nope, BaseAppState is not needed. I tried doing it so that it would automatically try to import something in com.jme3. It was just a test.

Options for creating a JME-using Java library:

  1. Create a JME project and then remove all of the stuff that you don’t need like the included application class, etc. and then ignore things like the assets folder, application building options, etc.
  2. Create a Java library project, right click on the project, add the JME-core jar dependency (and/or effects, plugins, etc.). Done.

(2) sounds easiest to me.

I used that all the time for the original Mythruna when I still used the SDK. You can then have your main game project depend on the other project just like a gradle multiproject build.