3

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?

4
  • 1
    The folder contains ' And the script uses ' to quote paths. Change the folder name from Matthew's Homeschooling Commented Sep 8, 2020 at 23:08
  • Thank you for locating this error. Is there some way for me to fix this error without changing my file name? Commented Sep 8, 2020 at 23:10
  • I have solved my problem by moving my files. But is there some way I can change the quote character that the script uses for paths? Commented Sep 8, 2020 at 23:44
  • 1
    @M-Chen-3,The path displayed before the real output is execution scripts and there's no way to change it, including full path that points to the Python interpreter to be used for debugging and the current file. but you can hide the execution script if you want. Commented Sep 10, 2020 at 8:33

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.