如何将Java代码制作成可执行程序?

huangapple 未分类评论49阅读模式
英文:

Super basic question: How does one make an executable program out of java code?

问题

我只编码了几天,有一个问题让我找不到任何信息。我想知道如何将我的Java代码创建成可执行应用程序。我知道可以在终端中键入java helloworld来运行编译后的代码,例如当我在正确的目录中时,但我总是不得不手动导航到文件并运行它。我希望有一种方法可以创建一个文件,我只需点击它,程序就会自动在计算机的终端中运行。

任何解释都将不胜感激!我目前在MacOS上工作,如果有所不同的话。

编辑:我使用Visual Studio Code编写我的代码。

编辑2:我现在明白我要达到的目标实质上是将我的.class文件创建为一个.jar文件。有人知道Visual Studio Code中是否有内置的功能来创建.jar文件吗?有些人建议使用Maven,但如果不确定是否有必要,我不太愿意下载任何东西。

英文:

I have only been coding for a few days and I have one question that i just can't seem to find any information on. I was wondering what I would have to do to create an executable application out of my java code. I know that I can run my compiled code from the terminal by typing java helloworld for example when I'm in the right directory, but then I always have to navigate to the file manually and run it. I was hoping there would be a way to create a file that I could just click on and that would automatically run the program in the computer's terminal.

Any explanation would be much appreciated! I'm currently working on MacOS if it makes a difference.
Thanks in advance.

edit: I'm using visual studio code to write my code

edit 2: I now understand that what I'm trying to accomplish is essentially to create a .jar file out of my .class file. Does anyone happen to know if there is a built in function in visual studio code to create a .Jar file? Some people are suggesting maven but I'm hesitant to download anything if im not sure it's necessary.

答案1

得分: 0

这会更有帮助,如果你正在使用像Eclipse或IntelliJ这样的集成开发环境,因为它们可以根据你的运行配置自动创建可执行的JAR文件。它们会自动创建文件,告诉你的系统在哪里找到主方法以便执行。

英文:

It would be more helpful to know if you are using an IDE like Eclipse or IntelliJ, as these can automatically create executable jar files from your run configurations.
They take care of automatically creating the files that tell your system where to find the main method to execute.

答案2

得分: -3

有两个步骤:

  1. javac helloworld.java:这将编译您的代码。

  2. java helloworld:这将在步骤1中创建的.class文件。

英文:

There are two steps:

  1. javac helloworld.java : This will compile your code.

  2. java helloworld : This will run the .class file created in step 1.

huangapple
  • 本文由 发表于 2020年5月29日 16:36:18
  • 转载请务必保留本文链接:https://java.coder-hub.com/62081868.html
匿名

发表评论

匿名网友

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

确定