Java 在 VS Code 中导出 jar 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51596602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:10:06  来源:igfitidea点击:

Exporting jar file in VS Code

javajarvisual-studio-codeexport

提问by Finn Frankis

I recently switched over to VS Code from Eclipse as my primary Java editor. I need to frequently export my code into a jar file which includes all libraries and dependencies for running on a Raspberry Pi. However, I currently see no obvious menus or tabs to accomplish this purpose.

我最近从 Eclipse 切换到 VS Code 作为我的主要 Java 编辑器。我需要经常将我的代码导出到一个 jar 文件中,其中包含在 Raspberry Pi 上运行的所有库和依赖项。但是,我目前看不到明显的菜单或选项卡来实现此目的。

Are there any extensions or built-in features which will behave in a manner similar to Eclipse's "Export" window? Would I be able to easily write a script to export for me?

是否有任何扩展或内置功能的行为方式类似于 Eclipse 的“导出”窗口?我能否轻松编写一个脚本来为我导出?

回答by Cesar

If your project has Maven, you can simply call mvn packageor mvn clean installfrom VS Code's embedded command line. Reference for Maven: https://cwiki.apache.org/confluence/display/MAVEN/Tutorial%3A+Build+a+JAR+file+with+Maven+in+5+minutes

如果您的项目有 Maven,您可以简单地从 VS Code 的嵌入式命令行调用mvn packagemvn clean install。Maven 参考:https: //cwiki.apache.org/confluence/display/MAVEN/Tutorial%3A+Build+a+JAR+file+with+Maven+in+5+minutes

For spring users in Windows type: .\mvnw package

对于 Windows 中的 spring 用户,请键入: .\mvnw package

EDIT: If you get 'mvn' is not recognized as an internal or external command, operable program or batch file.

编辑:如果你得到 'mvn' is not recognized as an internal or external command, operable program or batch file.

Then you need to: downloadhttps://maven.apache.org/download.cgiand installhttps://maven.apache.org/install.htmlmvn

然后你需要:downloadhttps: //maven.apache.org/download.cgiinstallhttps://maven.apache.org/install.htmlmvn

install is just setting the PATH env

安装只是设置 PATH 环境

回答by Calvin

回答by lazyTank

Let's say your project has app package. Under that a App.java class resides which has the main method.It also uses yourlib.jarlibrary. Now after building App.java file from vscode the class files let's assume the class file folder structure is

假设您的项目有应用程序包。在它下面有一个 App.java 类,它有 main 方法。它还使用yourlib.jar库。现在从 vscode 构建 App.java 文件后,类文件让我们假设类文件文件夹结构是

bin
   |app
       |App.class

Now go to the bin folder and make a manifest.txt file in bin folder. manifest.txt file must contain Main-Class . here app.App is the name of the Main-Class.Following can be the contents of manifest.txt file

现在转到 bin 文件夹并在 bin 文件夹中创建一个 manifest.txt 文件。manifest.txt 文件必须包含 Main-Class 。这里app.App是Main-Class的名字。下面可以是manifest.txt文件的内容

 Main-Class: app.App
 Class-Path: yourlib.jar

Notemanifest.txt file must be ended with a new line or carriage return . If your manifest file only contains Main-Class no Class-Path The after Main-Class: app.Appput a new line at least. Now copy the yourlib.jarfile in binand run this command from the binfolder

注意manifest.txt 文件必须以换行或回车结束。如果您的清单文件只包含 Main-Class 没有 Class-Path 之后Main-Class: app.App至少换行。现在将yourlib.jar文件复制到bin并从bin文件夹中运行此命令

jar cfmv App.jar manifest.txt app/

then test the Jar with

然后用

java -jar App.jar

Note:You can run and build class file by installing https://marketplace.visualstudio.com/items?itemName=redhat.javaextension.

注意:您可以通过安装https://marketplace.visualstudio.com/items?itemName=redhat.java扩展来运行和构建类文件。