简体   繁体   English

如何在 VSCode 中使用命令行参数运行 java?

[英]How do I run java with command line arguments in VSCode?

My employer has given me access to a site that teaches many things (not sure if allowed to post the name or not).我的雇主允许我访问一个教很多东西的网站(不确定是否允许发布姓名)。 I have decided to try and learn Java.我决定尝试学习 Java。 The way this site teaches it is through IntelliJ, but I have learned JavaScript, Python, Node, and React in VSCode, and I don't want to learn another system.这个网站的教学方式是通过IntelliJ,但是我已经在VSCode中学习了JavaScript、Python、Node和React,我不想学习其他系统。

Through Google, I downloaded JDK 11, as well as the Java Extension Pack.通过 Google,我下载了 JDK 11 以及 Java 扩展包。 I have had a few errors that I have been able to resolve, but I am now stuck on java App.java.我有一些我能够解决的错误,但我现在卡在 java App.java 上。 Earlier in the day, I was able to make it work, but now when I try to run javac App.java and then java com.example.App, it's not found.当天早些时候,我能够让它工作,但是现在当我尝试运行 javac App.java 然后 java com.example.App 时,它没有找到。 Nothing I have tried has made it work.我试过的任何东西都没有让它起作用。 Maybe it only worked before I used Maven...can't remember.也许它只在我使用 Maven 之前有效......不记得了。

On the topic of com.example.App, is there an easy way to create a package?关于com.example.App 的话题,有没有简单的创建包的方法? Google says it's almost always needed to create a package.谷歌表示几乎总是需要创建一个包。 Only way I've figured out how to do that is with Maven, but the quickstart version is 1.7 or 8 (when I have 11).我想出的唯一方法是使用 Maven,但快速入门版本是 1.7 或 8(当我有 11 时)。 So I have to go into the POM and edit所以我必须进入 POM 并进行编辑

  <properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

to

  <properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>

Is there a way to create a package without using Maven?有没有办法在不使用 Maven 的情况下创建包?

I just realized that I haven't even explained my title.我才意识到我什至没有解释我的标题。 So on the site I'm learning Java, it says there's a way to use command line arguments, for example:所以在我学习 Java 的网站上,它说有一种方法可以使用命令行参数,例如:

java com.example.App "I am an argument"

Since I can no longer run my code with java, I can not do that.因为我不能再用 java 运行我的代码,所以我不能这样做。 If anyone can help, you'd be amazing!如果有人能帮忙,你会很棒!

Q: Is there a way to create a package without using Maven?问:有没有不用 Maven 就可以创建包的方法?

I don't see any direct relation between the two.我看不出两者之间有任何直接关系。

Packages are the logical grouping of classes in a folder structure in Java whereas Maven is just a Java build tool.包是 Java 文件夹结构中类的逻辑分组,而 Maven 只是一个 Java 构建工具。

Yes, you can create packages without using Maven.是的,您可以在不使用 Maven 的情况下创建包。

If you are using any IDE, for example VS Code you can always create packages as below:如果您使用任何 IDE,例如 VS Code,您始终可以创建如下包:

  1. Create a Maven based Java project.创建一个基于 Maven 的 Java 项目。 Maven projects have the following structure src/main/java (all your source code goes here) and src/main/test (unit test goes here) , src/main/resources(non java files goes here). Maven 项目具有以下结构 src/main/java(所有源代码都放在这里)和 src/main/test(单元测试放在这里)、src/main/resources(非 java 文件放在这里)。

  2. Initially while creating the project you would have created a package with name com.example最初在创建项目时,您会创建一个名为 com.example 的包

  3. To create a new package, right click and select New folder要创建新包,请右键单击并选择新建文件夹

  4. Enter the folder name(this is your package name),for example "view".输入文件夹名称(这是您的包名称),例如“view”。

  5. Now you will have a new package com.example.vew.现在您将拥有一个新包 com.example.vew。 Similarly we can create packages at any folder level and place our Java file inside that.同样,我们可以在任何文件夹级别创建包并将我们的 Java 文件放在其中。

Let me know if you this is good enough for you.如果这对你来说足够好,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM