NetworkTables help

Hello i am new to gradle and have very little experience with it. I am trying to build NetworkTables to communicate between my Laptop and the roborio however when i type gradle build in my terminal i get this error

C:\Users\Desktop\GradleTest>gradle build
:compileJava
C:\Users\Desktop\GradleTest\src\main\com\djsprinkles\gradle\Main.java:3: error: package edu.wpi.first.wpilibj.networktables does not exist
import edu.wpi.first.wpilibj.networktables.NetworkTable;
                                          ^
C:\Users\Desktop\GradleTest\src\main\com\djsprinkles\gradle\Main.java:8: error: cannot find symbol
                NetworkTable.setClientMode();
                ^
  symbol:   variable NetworkTable
  location: class Main
C:\Users\Desktop\GradleTest\src\main\com\djsprinkles\gradle\Main.java:9: error: cannot find symbol
                NetworkTable.setTeam(4525);
                ^
  symbol:   variable NetworkTable
  location: class Main
C:\Users\Desktop\GradleTest\src\main\com\djsprinkles\gradle\Main.java:10: error: cannot find symbol
                NetworkTable.initialize();
                ^
  symbol:   variable NetworkTable
  location: class Main
C:\Users\Desktop\GradleTest\src\main\com\djsprinkles\gradle\Main.java:11: error: cannot find symbol
                NetworkTable table = NetworkTable.getTable("Test");
                ^
  symbol:   class NetworkTable
  location: class Main
C:\Users\Desktop\GradleTest\src\main\com\djsprinkles\gradle\Main.java:11: error: cannot find symbol
                NetworkTable table = NetworkTable.getTable("Test");
                                     ^
  symbol:   variable NetworkTable
  location: class Main
6 errors
:compileJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

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

BUILD FAILED

Total time: 1.488 secs

if anyone has a fix for this please tell me and also here is my gradle code keep in mind I’m new to gradle and may be missing something obvious


repositories{	
    maven {
      url "http://first.wpi.edu/FRC/roborio/maven/release"
    }
	
}

apply plugin: 'java'

dependencies{
	compile "edu.wpi.first.wpilib.networktables.java:NetworkTables:3.1.5"
}

sourceSets{
	main.java.srcDir "src/main"
	test.java.srcDir "src/test"
}

jar{
	manifest.attributes "Main-Class": "main.net.djsprinkles.gradle.Main"
}

and my code

package main.com.djsprinkles.gradle;

import edu.wpi.first.wpilibj.networktables.NetworkTable;

public class Main {

	public static void main(String] args) {
		NetworkTable.setClientMode();
		NetworkTable.setTeam(4525);
		NetworkTable.initialize();
		NetworkTable table = NetworkTable.getTable("Test");
		
		table.putNumber("X", 5);
		
	}

}

Thanks any help is appreciated.

I had exactly the same problem and any help would be appreciated.

I started with the example that you provided the link to and was able to get it to compile with the example gradle build.

However, when I started to use the classes for the library I noted that they had all been depricated and it was recommended that I use the new library.

Depricated:
import edu.wpi.first.wpilibj.networktable
New/Recommended:
import edu.wpi.first.networktables

This looks like the new library is not stored on the repository or its not named consistent with what is in the documentation?

Any help out there?

That’s an old version. You’ll need to use the new (as of last season) artifacts:


compile group: "edu.wpi.first.ntcore", name: "ntcore-java", version: "4.+"
runtime group: "edu.wpi.first.ntcore", name: "ntcore-jni", version: "4.+", classifier: "all"
runtime group: "edu.wpi.first.wpiutil", name: "wpiutil-java", version: "3.+", classifier: "all"

The Java library is a wrapper around a C++ library, hence the runtime dependency on “ntcore-jni” and its required library “wpiutil-java”. The “all” classifiers mean that they include the binaries for Windows, Mac, and Linux.

The new classes are in the “edu.wpi.first.networktables” package

Thanks for your help but your answer is totally greek to me.

I’m just looking for a proper import statement so that our code will find the correct classes… I thought it was the import statement:

import edu.wpi.first.networktables.*;

Obviously that does not work… which files should the statements you posted be noted be put in and where.

Those should be in the dependencies block in your build.gradle file. Your import statement is correct

So build.gradle:


// Maven repo + plugin setup, etc. here ...

dependencies {
  compile group: "edu.wpi.first.ntcore", name: "ntcore-java", version: "4.+"
  runtime group: "edu.wpi.first.ntcore", name: "ntcore-jni", version: "4.+", classifier: "all"
  runtime group: "edu.wpi.first.wpiutil", name: "wpiutil-java", version: "3.+", classifier: "all"
}

And in the Java files you want to use:


import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableInstance;

public class Main {

  NetworkTableInstance instance = NetworkTableInstance.getDefault();
  NetworkTable rootTable = instance.getTable("");

  // Do stuff with the table and instance

}


Thanks, I was able to get the code to compile without error.

I changed the dependencies in the gradle.build file to the following and commented out the ntcorDep() line… as the dependencies.gradle file refers to an old file for ntcoreDep().

dependencies {
// compile ntcoreDep()
compile group: “edu.wpi.first.ntcore”, name: “ntcore-java”, version: “4.+”
runtime group: “edu.wpi.first.ntcore”, name: “ntcore-jni”, version: “4.+”, classifier: “all”
// runtime group: “edu.wpi.first.wpiutil”, name: “wpiutil-java”, version: “3.+”, classifier: “all”
compile cscoreDep()
compile ‘org.opencv:opencv-java:3.1.0’
}

I also had to comment out the edu.wpi.first.wpiutil line because it could not find that file and provided the following message… I looked at the http: site and the …-all.jar file was not at that location.

> Could not find wpiutil-java-all.jar (edu.wpi.first.wpiutil:wpiutil-java:3.0.0).
Searched in the following locations:
http://first.wpi.edu/FRC/roborio/maven/release/edu/wpi/first/wpiutil/wpiutil-java/3.0.0/wpiutil-java-3.0.0-all.jar

I’m not certain that its worth migrating to the new networktables classes… all of the samples code on screensteps and other locations seems to still use the depreciate versions.

Grip no longer generates the code to write contour report to network tables. Is there an example of this?

Our team has successfully created a camera server that will compile and run on PI. We were able to successfully connect to the camera server from a driver’s station. We can call the grip pipeline java code generated successfully and it compiles. Next step is to write the contour report to a network table.

Ah, sorry about that. wpiutil doesn’t have an “all” classifier, so dropping that would fix it. That’s what I get for copy-paste

Grip no longer generates the code to write contour report to network tables. Is there an example of this?

GRIP never did that. It has only ever generated the OpenCV image processing steps. The publishing steps are purely for getting data from the GRIP application.

It’s pretty straightforward to implement yourself:


NetworkTable contoursTable = NetworkTableInstance.getDefault().getTable("/vision/contours");
GripPipeline gripPipeline = new GripPipeline();

...


List<MatOfPoint> contours = gripPipeline.getFilterContoursOutput();

List<Number> centerXs = new ArrayList<>();
List<Number> centerYs = new ArrayList<>();

for (MatOfPoint contour : contours) {
  Rect boundingRect = Imgproc.boundingRect(contour);
  centerXs.add(boundingRect.x + boundingRect.width / 2);
  centerYs.add(boundingRect.y + boundingRect.height / 2);
  // etc for width, height, ...
}

contoursTable.getEntry("centerX")
    .setNumberArray(centerXs.toArray(new Number[0]);
contoursTable.getEntry("centerY")
    .setNumberArray(centerYs.toArray(new Number[0)]);
// etc for width, height, ...

It’s much easier with streams and lambdas, but that’s a bit more advanced than what most high schools teach.

Thanks… This is a huge help… I’ve integrated sample code and have been able to compile with no errors… I’ll test with PI as soon as our team can get around to it.

I’m pretty new to Java and declaring variables inside a loop disturbed me a bit at first until I did a bit of research on line regarding this practice.

I’ve made progress but had a couple of problems when trying to run.

Without the addition of network tables and our grip pipeline the sample program downloaded from https://github.com/wpilibsuite/VisionBuildSamples/releases works as an mjpeg server.

I suspect that the run issue is related to the way the build works and it does not seem to include the ntcore*. jar file in the CameraVision-all.jar in the output.

First I added the cscore jar to my eclipse build path and I started seeing lists of methods and help when I enter “.” after objects. This helped a lot. We’re not building in eclipse but we do use it as an editor.

I added code to publish several variables to network tables, call the grip pipeline, and to do a few calculations and it compiles without error using ./gradlew build from powershell.

I copied the CameraVision.zip file from the “output” directory to the raspberry pi, unzipped it and tried to run with the “java -Djava.library.path=. -jar CameraVision-all.jar” script.

Initially I received the error “Error: Could not find or load main class Main …”
I tried to run the script many times hoping to get a different result and finally did:

pi@raspberrypi:~/vision $ java -Djava.library.path=. -jar CameraVision-all.jar
Exception in thread “main” java.lang.UnsatisfiedLinkError: no ntcore in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1864)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at edu.wpi.first.networktables.NetworkTablesJNI.<clinit>(NetworkTablesJNI.java:53)
at edu.wpi.first.networktables.NetworkTableInstance.getDefault(NetworkTableInstance.java:88)
at Main.main(Main.java:53)

This may be multiple problems but it appears ntcore is not included in the jar.

The CameraVision.zip has 3 files: runCameraVision, libopencv_java310.so, and CameraVIsion-all.jar.

I found another zip in the build\distributions folder with a CameraVision.zip file… it contains wpiutil-java-3.0.0.jar, opencv-java3.1.0.jar, ntcore-jni-4.0.0-all.jar, ntcore-java-4.0.0.jar, cscore-1.02-arm-raspbian.jar, and CameraVision.jar.

wpiutil probably is not needed as there are no methods used from it.

I expected the CameraVision-all.jar to include everything needed to run. Is the solution as simple as copying all of the jar files in the distributions folder (or selected ones) to the raspberry pi folder where I previously unzipped the CameraVision.zip file? Is something missing in the sample gradle.build that would include the jars in the build?

What is the difference between the CameraVision.jar and the CameraVision-all.jar? I read back through all of the post and noted that -all indicates build for all platforms (unix, windows, linux)… so the -all version will run on any platform?

I also note that there are no “arm-raspbian” versions of the jars other than for cscore (and maybe for those with -all suffix). Should there be or do these run on multiple platforms? I searched on the repository and did not find any raspbian versions of these.

I tried copying each of the additional jar’s to the PI where code runs with no success.

I don’t believe that is your problem. Our setup is working with a similar approach. Our deployable zip file to the PI only includes CameraVision.all, libopencv_java310.so and runCameraVision.

Are you trying to use the new version of network tables or the old version? The sample file from screensteps used the old version (its depricated). I’m trying to use the new version.

For now I’ve aborted using ntcore and have converted code to the depricated version. Compile runs fine and code loads and executes properly without errors.
We may be taking a performance hit by not using the latest code but at least it works.

We may try to get this to work later if we have any time before 1st competition.

Solution(s) found.

There were 2 problems.

  1. When grip wrote the pipeline code it asked for a java package and one was entered. In order to make the main class and the pipeline class pass the compiler checks, the same package statement was added to the main class file. This caused the runtime error " … main class “Main” not found"…
    Deleting the package statements from the top of both the main and pipeline class files fixed the problem.

  2. The runtime error …
    pi@raspberrypi:~/vision $ java -Djava.library.path=. -jar CameraVision-all.jar
    Exception in thread “main” java.lang.UnsatisfiedLinkError: no ntcore in java.library.path

was fixed by copying a libntcore.so, and libwpiutil.so file to the raspberry pi in the directory where the code is ran from. These had to be built from source for the PI and were graciously shared by Peter Johnson.

NTcore for raspbian was not being linked by the build or included in the zip file that the sample build created.