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.

1 Like

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

Your instructions herein got me close to running my program but I came up a little short.

I want to use the WPILib CameraServer class and I fail to include that correctly.

I guessed and added this implementation wpilibTools.deps.wpilibJava("cameraserver") to the build.gradle and the “interactive” Java in VSCode works.

The Java compile in .\gradle shadowJar could not find the CameraServer jars so I copied them from the WPILib maven on my laptop to the sample project and the Java compiler found them (I’m being bad but I don’t know how to do it better).

Then the fatal error is running the sample using the CameraServer I get this error:

java.io.IOException: cscorejni could not be loaded from path or an embedded resource.
attempted to load for platform /windows/x86-64/
Last Load Error:
no cscorejni in java.library.path:

more path of the error message

C:\Users\Public\wpilib\2023\jdk\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Users\Public\wpilib\2023\jdk\bin;C:\Users\Public\wpilib\2023\jdk\bin;C:\Users\Public\wpilib\2023\jdk\bin;C:\Users\Public\wpilib\2023\jdk\bin;C:\Users\Public\wpilib\2023\jdk\bin;C:\Users\Public\wpilib\2023\jdk\bin;C:\Program Files\National Instruments\Shared\OpenVINO;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64;C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin;C:\Program Files\apache-maven-3.3.9\bin;%CMAKE_DIR%/bin;C:\opencv\build\java\x64;c:\ffmpeg\bin;C:\Program Files\Java\jdk-12\bin;C:\WINDOWS\System32\OpenSSH;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program Files\Microsoft Network Monitor 3;C:\Users\RKT\AppData\Local\Programs\Python\Python39\Scripts;C:\Users\RKT\AppData\Local\Programs\Python\Python39;C:\Users\RKT\AppData\Local\Microsoft\WindowsApps;C:\Users\RKT\AppData\Local\Microsoft\WindowsApps;C:\Users\RKT\AppData\Local\Programs\Microsoft VS Code\bin;C:\Program Files\JetBrains\PyCharm Community Edition 2020.3.2\bin;;C:\Users\RKT\AppData\Local\Programs\MiKTeX\miktex\bin\x64;.
A common cause of this error is missing the C++ runtime.
Download the latest at Latest supported Visual C++ Redistributable downloads | Microsoft Learn

    at edu.wpi.first.util.RuntimeLoader.loadLibrary(RuntimeLoader.java:93)
    at edu.wpi.first.cscore.CameraServerJNI.forceLoad(CameraServerJNI.java:58)
    at edu.wpi.first.cscore.CameraServerCvJNI.<clinit>(CameraServerCvJNI.java:33)
    at edu.wpi.first.cscore.CvSource.<init>(CvSource.java:36)
    at edu.wpi.first.cameraserver.CameraServer.putVideo(CameraServer.java:776)
    at Program.main(Program.java:47)

In the TestApplication-winx64.jar\windows\x86-64\shared\ there is a cscorejnicvstatic.dll and several others but no cscorejni.dll

My flailing got me well along but I don’t know what to do next. Can you point me in the right direction? thanks.

I did get OpenCV added and that works.

I want to run an OpenCV program and I added that successfully to the build.gradle (albeit crudely done by copying the jar and dll files into the sample project and adding this implementation fileTree(dir: 'libs', include: ['*.jar'])). I think I’ll be able to point to where I actually installed OpenCV when I refactor this.

:fireworks: :sparkler: :firecracker:

Make sure you’re doing in your main() what the template project does in main()… it has code to load the native libraries in Program.java

I took the StandaloneApp Server java code and added implementation wpilibTools.deps.wpilibJava("cameraserver") to build.gradle and the following to Program.java and got camera images from my webcam on SmartDashboard.

        var inst = NetworkTableInstance.getDefault();
        inst.startServer();

        CameraServer.startAutomaticCapture();

        while(true);

Thus you’re probably missing something that’s in the example. I’d start with what Peter identified.

Edit: after adding more stuff that uses OpenCV, I get the same error message, so it may not be because you’re missing something from the example.