Problems with importing wpilib (java)

Hi, I am working on some app that will pull data from networktables4 and analyze it and put into graphs, but i cannot even start with the basic code from

I struggled to get gradle running on my VSCode and I get errors on imports.
My gradle.build looks like this

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This is a general purpose Gradle build.
 * Learn more about Gradle by exploring our samples at https://docs.gradle.org/7.6/samples
 */

dependencies {
    // Add ntcore-java
    implementation "edu.wpi.first.ntcore:ntcore-java:2023.2.1"

    // Add wpiutil-java
    implementation "edu.wpi.first.wpiutil:wpiutil-java:2023.2.1"

    // Add ntcore-jni for runtime. We are adding all supported platforms
    // so that our application will work on all supported platforms.
    implementation "edu.wpi.first.ntcore:ntcore-jni:2023.2.1:windowsx86"
    implementation "edu.wpi.first.ntcore:ntcore-jni:2023.2.1:windowsx86-64"
    implementation "edu.wpi.first.ntcore:ntcore-jni:2023.2.1:linuxx86-64"
    implementation "edu.wpi.first.ntcore:ntcore-jni:2023.2.1:linuxraspbian"
    implementation "edu.wpi.first.ntcore:ntcore-jni:2023.2.1:osxx86-64"
}

I want to be able to import libraries from wpilib so i can use it in my program. I’m new to doing something like this. Appreciate any help.
(I tried different implementation or compiles in build gradle and they didnt work, but maybe they were too old.)

* Where:
Build file 'D:\_Programming\frcstattrack\build.gradle' line: 10

* What went wrong:
A problem occurred evaluating root project 'frcstattrack'.
> Could not find method implementation() for arguments [edu.wpi.first.ntcore:ntcore-java:2023.2.1] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

That build.gradle example is broken for other reasons (it’s not longer viable to just use ntcore-jni). We’re working on building a better template for standalone applications here: GitHub - wpilibsuite/StandaloneAppSamples: App Samples for running apps standalone but haven’t updated the docs yet to point to it.

is there a way to just be able to import all wpilib libraries to this standalone app so i can use them right now? I wanted to start working on it, it’s something rather easy, but i need the basics.

It imports the critical ones including ntcore for NetworkTables and cscore for cameras.

But i can’t make this code work on my pc

(i didnt use the maven, i didnt install it)
i get something like this when i run it:

PS D:\_Programming\frcstattrack> gradle build

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'frcstattrack'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve edu.wpi.first:GradleRIO:2023.2.1.
     Required by:
         project : > edu.wpi.first.GradleRIO:edu.wpi.first.GradleRIO.gradle.plugin:2023.2.1
      > No matching variant of edu.wpi.first:GradleRIO:2023.2.1 was found. The consumer was configured to find a runtime of a library compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.6' but:
          - Variant 'apiElements' capability edu.wpi.first:GradleRIO:2023.2.1 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
          - Variant 'runtimeElements' capability edu.wpi.first:GradleRIO:2023.2.1 declares a runtime of a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
   > Could not resolve edu.wpi.first:wpilib-tool-plugin:1.1.0.
     Required by:
         project : > edu.wpi.first.WpilibTools:edu.wpi.first.WpilibTools.gradle.plugin:1.1.0
      > No matching variant of edu.wpi.first:wpilib-tool-plugin:1.1.0 was found. The consumer was configured to find a runtime of a library compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.6' but:
          - Variant 'apiElements' capability edu.wpi.first:wpilib-tool-plugin:1.1.0 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
          - Variant 'javadocElements' capability edu.wpi.first:wpilib-tool-plugin:1.1.0 declares a runtime of a component, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
          - Variant 'runtimeElements' capability edu.wpi.first:wpilib-tool-plugin:1.1.0 declares a runtime of a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.6')
          - Variant 'sourcesElements' capability edu.wpi.first:wpilib-tool-plugin:1.1.0 declares a runtime of a component, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.6')

It looks like you’re running Java 8? You need at least 11, and 17 would be better. Check your JAVA_HOME and PATH. The WPILib installer installs a 17 JDK in C:\Users\Public\wpilib. Also, you need to run .\gradlew.bat, not gradle.

Got my java to 17,
I dont know whats going on with theese

PS D:\_Programming\frcstattrack> .\gradlew build
Starting a Gradle Daemon, 3 incompatible Daemons could not be reused, use --status for details

FAILURE: Build failed with an exception.

* Where:
Build file 'D:\_Programming\frcstattrack\build.gradle' line: 13

* What went wrong:
A problem occurred evaluating root project 'frcstattrack'.
> Could not find method implementation() for arguments [DefaultExternalModuleDependency{group='edu.wpi.first.wpiutil', name='wpiutil-java', version='+', configuration='default'}] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

I would recommend starting from the StandaloneAppSamples project and adding your .java files to it. There’s no implementation() line on line 13 of its build.gradle.

To elaborate a bit more on the above, whatever project you’re adding things to seems to be running on gradle 5 (I think, implementation might have been an even earlier change then that.) I’d definitely recommend just taking a copy of the Java folder, and adding your code to to that.

You can set the jar name on line 42 of build.gradle. Then you can either run the code directly with ./gradlew run or you can get a jar you can run manually with ./gradlew shadowJar.

that repo code worked well on my pc, but this current docs code worked like this:
(i installed c++ runtime from this link)

with gradle.build looking like this:
(commented things, i thought i wouldnt need)

plugins {
    id "java"
    id 'application'
    // id 'com.github.johnrengelman.shadow' version '7.1.2'
    id "edu.wpi.first.GradleRIO" version "2023.2.1"
    id 'edu.wpi.first.WpilibTools' version '1.1.0'
}

mainClassName = 'Program'

wpilibTools.deps.wpilibVersion = wpi.versions.wpilibVersion.get()

def nativeConfigName = 'wpilibNatives'
def nativeConfig = configurations.create(nativeConfigName)

def nativeTasks = wpilibTools.createExtractionTasks {
    configurationName = nativeConfigName
}

nativeTasks.addToSourceSetResources(sourceSets.main)
nativeConfig.dependencies.add wpilibTools.deps.wpilib("wpimath")
nativeConfig.dependencies.add wpilibTools.deps.wpilib("wpinet")
nativeConfig.dependencies.add wpilibTools.deps.wpilib("wpiutil")
nativeConfig.dependencies.add wpilibTools.deps.wpilib("ntcore")
nativeConfig.dependencies.add wpilibTools.deps.cscore()

dependencies {
    implementation wpilibTools.deps.wpilibJava("wpiutil")
    implementation wpilibTools.deps.wpilibJava("wpimath")
    implementation wpilibTools.deps.wpilibJava("wpinet")
    implementation wpilibTools.deps.wpilibJava("ntcore")
    implementation wpilibTools.deps.wpilibJava("cscore")

    // implementation group: "com.fasterxml.jackson.core", name: "jackson-annotations", version: wpi.versions.jacksonVersion.get()
    // implementation group: "com.fasterxml.jackson.core", name: "jackson-core", version: wpi.versions.jacksonVersion.get()
    // implementation group: "com.fasterxml.jackson.core", name: "jackson-databind", version: wpi.versions.jacksonVersion.get()

    // implementation group: "org.ejml", name: "ejml-simple", version: wpi.versions.ejmlVersion.get()
}

// shadowJar {
//     archiveBaseName = "TestApplication"
//     archiveVersion = ""
//     exclude("module-info.class")
//     archiveClassifier.set(wpilibTools.currentPlatform.platformName)
// }

wrapper {
    gradleVersion = '7.5.1'
}

You need to pull in the first lines of that repo’s Program.java main() function into your main() (along with the corresponding imports).

Now it runs well. When running by just run in vs code it throws:

PS D:\_Programming\Java>  d:; cd 'd:\_Programming\Java'; & 'C:\Users\dylew\.vscode\extensions\redhat.java-1.14.0-win32-x64\jre\17.0.5-win32-x86_64\bin\java.exe' '@C:\Users\dylew\AppData\Local\Temp\cp_2w4ct53ve5c7pf9uno36a3n3w.argfile' 'Program'
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/type/TypeReference
        at Program.main(Program.java:25)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.type.TypeReference
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
        at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more
PS D:\_Programming\Java>

But it is just exception, and after
./gradlew build
it says build successful!

I don’t know if the exception above is sth important.

Uncomment the implementation lines re: jackson in the build.gradle.

everything uncommented

After uncommenting, its possible you’ll need to refresh the java workspace to get things working in VS Code. Run the Clean Java Workspace command from the command palette in VS Code to do this.

YAY!
Thanks guys, if i have something strange later, i’ll post it in this thread.
Also, I can use this code from repo GitHub - wpilibsuite/StandaloneAppSamples: App Samples for running apps standalone for my own app right? (do i need to implement something somewhere for credit)

Go ahead and use it all you want. Its the template to do exactly what you need, you can change it however you like.

1 Like