Search code examples
javaenvironment-variablescentoswindows-subsystem-for-linux

Cannot Run Java in CentOS WSL2: Error load main class Files.NVIDIA


I tried to run a shell script to run an application in Java. I cannot post the whole script due to copyright. But the line executing Java is the following (with some modification)

$java_vm -Xmx128m -classpath "$CLASSPATH" -DDISPLAY=$DISPLAY -DPATH=$PATH -Djava.library.path=. -DEXEC_PLAT=$plat -jar $kp_dir/lib/SomeJAR.jar $args 2>&1

where $java_vm is the path to java

When executing the script, I got the following error:

Error: Could not find or load main class Files.NVIDIA Caused by: java.lang.ClassNotFoundException: Files.NVIDIA

I run this in WSL2 CentOS system. I found that this error could be fixed by removing -DPATH=$PATH option altogether. However, doing so will remove the application ability to access the internet (maybe because some important path for networking is removed)

Here is my PATH variable

/usr/lib/jvm/java-17-openjdk-17.0.1.0.12-2.el8_5.x86_64/bin:/usr/lib/jvm/jre-17-openjdk-17.0.1.0.12-2.el8_5.x86_64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program Files/WireGuard/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/usbipd-win/:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/Users/myName/AppData/Local/Programs/Python/Python312/Scripts/:/mnt/c/Users/myName/AppData/Local/Programs/Python/Python312/:/mnt/c/Users/myName/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/myName/AppData/Local/Programs/Microsoft VS Code/bin:/mnt/c/Users/myName/AppData/Local/GitHubDesktop/bin:/mnt/c/Program Files/KS-Wireshark:/root/bin

I tried to run a shell script in WSL2 CentOS system. I expect it to run normally but I got this error which can be solved by removing -DPATH option. But doing so will remove the application networking capability


Solution

  • It is because java won't understand your PATH variable as one command(or arguemtns), but as multiple arguments because of space.

    for example, the part of the path makes error occured: :/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR

    the command JVM understand will look like: java_vm -Xmx128m -classpath "$CLASSPATH" -DDISPLAY=$DISPLAY -DPATH=:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR which JVM will only recognize :/mnt/c/Program Files/NVIDIA as the only path you entered, and Files/NVIDIA becomes the single argument, which indicates main class's name.

    To solve this, add " when using variable, like "$PATH", and this will prevent space to be parsed from command parser.