Edu.wpi.first.wpilibj.simulation.* cannot be imported

A quick bit of background. Our team has been using Python for the past few years, and we just got the amazing VMX-Pi from Kuai. In fact, it is so cool, I am considering using it to start teaching Java to the team. In light of that I am working on, one of my many projects during our remote learning time, refreshing my Java skills. I thought I would work primarily in the simulator for now (and therefore use the 2020 WPILib) to practice.

One of the coolest aspects of RobotPy is the 2d field sim, and I see it is supported in Java too.

However, when I attempt to import it, VsCode does not recognize the library. When I attempt to compile and build, it fails because it cannot find the library (This is true even if I only import it)…

I am using WPILib v. 2020.3.2.

> Task :compileJava FAILED
...\TankSim\TankSim\src\main\java\frc\robot\ error: package edu.wpi.first.wpilibj.simulation does not exist
import edu.wpi.first.wpilibj.simulation.Field2d;       
1 error
Compilation Error!
GradleRIO detected this build failed due to a Compile Error (compileJava).
Check that all your files are saved, then scroll up in 
this log for more information.

FAILURE: Build failed with an exception.

Does anyone have any suggestions? I do have WPILib2019 installed as well (for interfacing with the VMX-Pi). Yet, the help docs seemed to say that was a possibility. Is that in fact true, is there anything else I could have done to corrupt my WPILib install?

I am thinking of just clearing out both versions and starting from scratch, but wanted to ask here first.

Here is my Gradle file.

plugins {
    id "java"
    id "edu.wpi.first.GradleRIO" version "2020.3.2"

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

def ROBOT_MAIN_CLASS = "frc.robot.Main"

// Define my targets (RoboRIO) and artifacts (deployable files)
// This is added by GradleRIO's backing project EmbeddedTools.
deploy {
    targets {
        roboRIO("roborio") {
            // Team number is loaded either from the .wpilib/wpilib_preferences.json
            // or from command line. If not found an exception will be thrown.
            // You can use getTeamOrDefault(team) instead of getTeamNumber if you
            // want to store a team number in this file.
            team = frc.getTeamNumber()
    artifacts {
        frcJavaArtifact('frcJava') {
            targets << "roborio"
            // Debug can be overridden by command line, for use with VSCode
            debug = frc.getDebugOrDefault(false)
        // Built in artifact to deploy arbitrary files to the roboRIO.
        fileTreeArtifact('frcStaticFileDeploy') {
            // The directory below is the local directory to deploy
            files = fileTree(dir: 'src/main/deploy')
            // Deploy to RoboRIO target, into /home/lvuser/deploy
            targets << "roborio"
            directory = '/home/lvuser/deploy'

// Set this to true to enable desktop support.
def includeDesktopSupport = true

// Defining my dependencies. In this case, WPILib (+ friends), and vendor libraries.
// Also defines JUnit 4.
dependencies {
    implementation wpi.deps.wpilib()
    nativeZip wpi.deps.wpilibJni(wpi.platforms.roborio)
    nativeDesktopZip wpi.deps.wpilibJni(wpi.platforms.desktop)

    nativeZip wpi.deps.vendor.jni(wpi.platforms.roborio)
    nativeDesktopZip wpi.deps.vendor.jni(wpi.platforms.desktop)

    testImplementation 'junit:junit:4.12'

    // Enable simulation gui support. Must check the box in vscode to enable support
    // upon debugging
    simulation wpi.deps.sim.gui(wpi.platforms.desktop, false)

// Setting up my Jar File. In this case, adding all libraries into the main jar ('fat jar')
// in order to make them all available at runtime. Also adding the manifest so WPILib
// knows where to look for our Robot Class.
jar {
    from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
    manifest edu.wpi.first.gradlerio.GradleRIOPlugin.javaManifest(ROBOT_MAIN_CLASS)

Thank you.
~Mr. R^2

Field2d is in the wrong spot in 2020.3.2, so it can’t be imported at all. The fix for now is to just copy the Field2d file from wpilib into your project. You would then repackage it to be part of your robot project.

1 Like

Thank you. That makes sense. I am not certain I know how to repackage it, but I will try my hand and post specifics if I have specific questions.

Edit: Yep, That was as easy as it could be. Thank you. If anyone else is curious, all I needed to do was copy the file into my project, change the package name to my package frc.robot and then import it…
import frc.robot.Field2d

Thank you again.