Hal.report() crashing my code when I try to run junit

CODE:

import edu.wpi.first.wpilibj.controller.PIDController;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class Yes {

@Before
public void setup() {
    PIDController pidController = new PIDController(0.23, 0, 0, 1);
}

@Test
public void yes() {
    Assert.assertEquals(0, 0, 0);
}

}

ERROR:

java.io.IOException: wpiHaljni could not be loaded from path or an embedded resource.
attempted to load for platform /windows/x86-64/
Last Load Error:
no wpiHaljni in java.library.path: [C:\Users\saarz.jdks\corretto-11.0.11\bin, C:\WINDOWS\Sun\Java\bin, C:\WINDOWS\system32, C:\WINDOWS, C:\Python39\Scripts, C:\Python39, C:\Program Files (x86)\Razer\ChromaBroadcast\bin, C:\Program Files\Razer\ChromaBroadcast\bin, C:\Program Files (x86)\Common Files\Oracle\Java\javapath, C:\Windows\system32, C:\Windows, C:\Windows\System32\Wbem, C:\Windows\System32\WindowsPowerShell\v1.0, C:\Windows\System32\OpenSSH, C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common, C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR, C:\WINDOWS\system32, C:\WINDOWS, C:\WINDOWS\System32\Wbem, C:\WINDOWS\System32\WindowsPowerShell\v1.0, C:\WINDOWS\System32\OpenSSH, C:\Program Files\Git\cmd, C:\Users\saarz\AppData\Local\Microsoft\WindowsApps, C:\Users\saarz\AppData\Local\gitkraken\bin, C:\Program Files\dotnet, C:\Program Files\nodejs, C:\ProgramData\chocolatey\bin, C:\Users\saarz\AppData\Local\Microsoft\WindowsApps, C:\Users\saarz\AppData\Local\gitkraken\bin, C:\Users\saarz.dotnet\tools, C:\Users\saarz\AppData\Roaming\npm, C:\Program Files\nodejs, ., C:\Users\saarz\AppData\Local\Programs\Microsoft VS Code\bin, .]
A common cause of this error is missing the C++ runtime.
Download the latest at https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads

at edu.wpi.first.wpiutil.RuntimeLoader.loadLibrary(RuntimeLoader.java:79)
at edu.wpi.first.hal.JNIWrapper.<clinit>(JNIWrapper.java:34)
at edu.wpi.first.wpilibj.controller.PIDController.<init>(PIDController.java:92)
at Yes.setup(Yes.java:13)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

Process finished with exit code 1

Configuration is with JUnit

When running unit tests, you need to call HAL.initialize() before anything else. In regular operations RobotBase takes care of it for you. There’s an frc-docs article about unit tests, I suggest reading it.

I tried to do this and this is what happens:

CODE:

@Before
public void setup() {
    HAL.initialize(100, 0);
    PIDController pidController = new PIDController(0.23, 0, 0, 1);
}

ERROR:
java.io.IOException: wpiHaljni could not be loaded from path or an embedded resource.
attempted to load for platform /windows/x86-64/
Last Load Error:
no wpiHaljni in java.library.path: [C:\Users\saarz.jdks\corretto-11.0.11\bin, C:\WINDOWS\Sun\Java\bin, C:\WINDOWS\system32, C:\WINDOWS, C:\Python39\Scripts, C:\Python39, C:\Program Files (x86)\Razer\ChromaBroadcast\bin, C:\Program Files\Razer\ChromaBroadcast\bin, C:\Program Files (x86)\Common Files\Oracle\Java\javapath, C:\Windows\system32, C:\Windows, C:\Windows\System32\Wbem, C:\Windows\System32\WindowsPowerShell\v1.0, C:\Windows\System32\OpenSSH, C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common, C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR, C:\WINDOWS\system32, C:\WINDOWS, C:\WINDOWS\System32\Wbem, C:\WINDOWS\System32\WindowsPowerShell\v1.0, C:\WINDOWS\System32\OpenSSH, C:\Program Files\Git\cmd, C:\Users\saarz\AppData\Local\Microsoft\WindowsApps, C:\Users\saarz\AppData\Local\gitkraken\bin, C:\Program Files\dotnet, C:\Program Files\nodejs, C:\ProgramData\chocolatey\bin, C:\Users\saarz\AppData\Local\Microsoft\WindowsApps, C:\Users\saarz\AppData\Local\gitkraken\bin, C:\Users\saarz.dotnet\tools, C:\Users\saarz\AppData\Roaming\npm, C:\Program Files\nodejs, ., C:\Users\saarz\AppData\Local\Programs\Microsoft VS Code\bin, .]
A common cause of this error is missing the C++ runtime.
Download the latest at https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads

at edu.wpi.first.wpiutil.RuntimeLoader.loadLibrary(RuntimeLoader.java:79)
at edu.wpi.first.hal.JNIWrapper.<clinit>(JNIWrapper.java:34)
at Yes.setup(Yes.java:11)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

And I’ve also tried to do what it tells me to do and download C++ runtime and it didn’t change anything

Did you install WPILib (in a version that matches the one specified in the build.gradle)?

What do you mean? how do I do that. I am running the java class with JUnit. Do I need to run it with gradle?

Yes. Gradle takes care of extracting the native libraries and setting up Java to find them. See Unit Testing — FIRST Robotics Competition documentation (running tests section)

I have a question about this answer. I agree if you follow those instructions, you do get the test runner to run the tests written.

How do we debug test failures though? Pressing F1 and running “WpiLib: Test Robot Code” executes the test cases, but there is no debugger possible when there are failures.

I tried installing the standard VsCode extension “Java Test Runner”, and it executes tests that are pure java code. If we start to get into the HAL or NetworkTables though, I get the same “no wpiHaljni in java.library.path”. I obviously have VC++ runtime, as the tests run in command line. Is there a way we can get the JNI paths in standard gradle to debug tests?

See this link for how to get it working. For 2022 templates this should be fixed

Thanks! It works great!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.