Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem running Java programs in Visual Studio Code

I'm trying to make Java programs work on Visual Studio Code. I've downloaded the Microsoft extension, downloaded the JKE from Oracle, and now I'm trying to run the proverbial "Hello World" Java program to make sure it works. Here is my program (I got it from the "Get Started" tutorial so it should be fine):

class QuickStart {
    public static void main(final String[] args) {
        System.out.println("Hello, World.");
    }
}

Here's what happens when I run it:

(my files)>  cd 'c:\Users\Janet\Documents\Matthew's Homeschooling\VSC Programs\APCSA'; & 
'c:\Users\Janet\.vscode\extensions\vscjava.vscode-java-debug-0.28.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-14.0.2\bin\java.exe' '--enable-preview' '-XX:+ShowCodeDetailsInExceptionMessages' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\Janet\AppData\Roaming\Code\User\workspaceStorage\5eda06847dc66aa5b01dbd290d4e0d18\redhat.java\jdt_ws\APCSA_a6725e29\bin' 'QuickStart'
>>

As you can see, it enters some weird shell. Then when I try running it again, I get this error:

>>  cd 'c:\Users\Janet\Documents\Matthew's Homeschooling\VSC Programs\APCSA'; & 'c:\Users\Janet\.vscode\extensions\vscjava.vscode-java-debug-0.28.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-14.0.2\bin\java.exe' '--enable-preview' '-XX:+ShowCodeDetailsInExceptionMessages' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\Janet\AppData\Roaming\Code\User\workspaceStorage\5eda06847dc66aa5b01dbd290d4e0d18\redhat.java\jdt_ws\APCSA_a6725e29\bin' 'QuickStart'
Set-Location : A positional parameter cannot be found that accepts argument 's'.
At line:1 char:2
+  cd 'c:\Users\Janet\Documents\Matthew's Homeschooling\VSC Programs\AP ...
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Location], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
 
Hello, World.

So it does print out "Hello World.", but it spits out an error and I have to run it twice. How can I run my Java program without errors by running it once?

like image 499
M-Chen-3 Avatar asked Jul 11 '26 02:07

M-Chen-3


1 Answers

The answer to this problem is to double check, nay, triple check your file names. The problem is that with Visual Studio Code's way of running programs (that is hitting the run button), any ' can cause serious problems. This is due to the fact that VS Code uses ' to quote file paths. Thanks to Eliott Frisch for pointing this out to me.

There are two possible solutions to this:

  1. Change your folder name to remove the problematic character. In my case, however, my folder was running programs so I couldn't do that. This leads me to...

  2. Create a new folder and move your programs there. This is, in my opinion, the best solution since you generally want a separate folder for your programs anyway.

Note that this is especially important for Java programs, since in Visual Studio Code you can't run Java programs with java name.java. Similarly, this is not important for Python programs, since those can be run with python name.py and therefore don't require any file names.

like image 53
M-Chen-3 Avatar answered Jul 14 '26 03:07

M-Chen-3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!