问题在于在 Visual Studio Code 中运行 Java 程序。

huangapple go评论118阅读模式
英文:

Problem running Java programs in Visual Studio Code

问题

以下是翻译好的部分:

我正在尝试在Visual Studio Code上运行Java程序。我已经下载了Microsoft的扩展,从Oracle下载了JKE,现在我正在尝试运行谚语中的“Hello World” Java程序,以确保它可以正常工作。以下是我的程序(我从“入门”教程中获取的,所以应该没问题):

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

当我运行它时,发生了以下情况:

(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\workspaceStorageeda06847dc66aa5b01dbd290d4e0d18\redhat.java\jdt_ws\APCSA_a6725e29\bin' 'QuickStart'
>>

正如您所见,它进入了一些奇怪的shell。然后,当我再次尝试运行它时,我会得到以下错误:

>> 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\workspaceStorageeda06847dc66aa5b01dbd290d4e0d18\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.

所以它确实输出了“Hello World”,但同时也会抛出一个错误,而且我必须运行两次。如何能够在运行一次的情况下无错误地运行Java程序呢?

英文:

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\workspaceStorageeda06847dc66aa5b01dbd290d4e0d18\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\workspaceStorageeda06847dc66aa5b01dbd290d4e0d18\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?

答案1

得分: 1

这个问题的解决方法是要仔细检查,甚至是三重检查你的文件名。问题在于使用Visual Studio Code运行程序的方式(即点击运行按钮),任何'都可能引起严重问题。这是因为VS Code使用'来引用文件路径。感谢Eliott Frisch向我指出这一点。

对于这个问题有两种可能的解决方案:

  1. 将文件夹名更改为删除有问题的字符。然而,在我的情况下,我的文件夹正在运行程序,所以我不能这样做。这就引导我到...

  2. 创建一个新文件夹,将你的程序移动到那里。这在我看来是最好的解决方案,因为通常你都希望为你的程序单独创建一个文件夹。

请注意,这对于Java程序尤其重要,因为在Visual Studio Code中你不能使用java name.java来运行Java程序。类似地,对于Python程序来说这就不重要,因为这些程序可以使用python name.py来运行,因此不需要任何文件名。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月9日 06:47:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63802512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定