Java 如何从 Netbeans 中在远程服务器上部署 jar 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21184441/
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-13 07:10:20  来源:igfitidea点击:

How to Deploy a jar File on a remote server from within Netbeans?

javanetbeansdeployment

提问by EsmaeelQash

I have developed a server [server.jar], and it is now working well (on my localhost). Now I want to "put" this server.jar on my remote server (ie: 122.152.12.33) and run it there, so My client application Replace "Localhost" with this ip and connect the [server.jar]...

我开发了一个服务器 [server.jar],它现在运行良好(在我的本地主机上)。现在我想把这个 server.jar 放在我的远程服务器上(即:122.152.12.33)并在那里运行它,所以我的客户端应用程序用这个 ip 替换“Localhost”并连接 [server.jar]...

How may I do that?

我该怎么做?

I found answers but didn't understand them (WAR? , and some configurations).

我找到了答案但不明白它们(WAR?,以及一些配置)。

I am using NetBeans IDE. Deployment of Jar file and converting to war using netbeans.

我正在使用 NetBeans IDE。使用 netbeans 部署 Jar 文件并转换为 war。

NoteI also use Mysql Database that I must upload it on the server side.

注意我也使用 Mysql 数据库,我必须在服务器端上传它。

*Update:Another related Question:

*更新:另一个相关问题:

If I want to try my server.jar on an existing website (Wordpress released site), just by adding my project in a folder, may this harm the online site?

如果我想在现有网站(Wordpress 发布的网站)上尝试我的 server.jar,只需将我的项目添加到文件夹中,这是否会损害在线网站?

And may I use php to run the Jar? (the site that is online is Link. I want to a create a folder http://akhbarna.com/MyPrject/... and then put the Jar there and run it by Php. Is it possible? And how this may effect the Online site?

我可以使用 php 来运行 Jar 吗?(在线站点是Link。我想创建一个文件夹http://akhbarna.com/MyPrject/...然后将 Jar 放在那里并通过 Php 运行它。这可能吗?这可能会产生什么影响在线网站?

采纳答案by Nick Holt

Running in your IDE is a long way from released and working ;-)

在您的 IDE 中运行距离发布和工作还有很长的路要走 ;-)

Your question is quite general, so my answer is quite general too but covers the basic approach.

你的问题很笼统,所以我的回答也很笼统,但涵盖了基本方法。

The key to this is thinking what would be needed to run a hello world application on a brand new desktop PC - you'd need to install Java, create a folder to contain your application and some scripts that run the program then copy your jar to the new desktop PC and run those scripts. It's no different on the server, only you'll probably be using 3rd party libraries and will probably install more than just Java.

关键是考虑在全新的台式机上运行 hello world 应用程序需要什么 - 您需要安装 Java,创建一个文件夹来包含您的应用程序和一些运行该程序的脚本,然后将您的 jar 复制到新的台式机并运行这些脚本。在服务器上没有什么不同,只是您可能会使用 3rd 方库并且可能安装的不仅仅是 Java。

Before we get to some more details, first off, the terms in the build, release and deployment space tend to be horribly overloaded. To clarify, in the following answer I mean the following:

在我们了解更多细节之前,首先,构建、发布和部署空间中的术语往往超载得可怕。为了澄清,在以下答案中,我的意思如下:

  • artifact - the jar, war, zip, package, etc produced by a project.
  • release - building your code to create an artifact and deploying it.
  • deploy - uploading an artifact to a repository.
  • install - taking an artifact from a repository and putting on a server so that it can be run.
  • artifact - 项目生成的 jar、war、zip、包等。
  • 发布 - 构建您的代码以创建工件并部署它。
  • 部署 - 将工件上传到存储库。
  • 安装 - 从存储库中取出工件并放在服务器上以便它可以运行。

I'll assume you're starting from scratch and are working in a professional environment.

我假设您是从头开始并在专业环境中工作。

First thing to do is prepare your server. Like you did with your desktop, you're going to need to install some software - at a bare minimum Java and MySQL plus a Servlet container such as Tomcat if you're building a web-app. You've got two options, either you do these installations manually or use a tool like Puppetto automate them - I'd recommend the later because once setup you can build a server in minutes rather than days.

首先要做的是准备您的服务器。就像你在你的桌面上所做的那样,你需要安装一些软件——如果你正在构建一个 web 应用程序,你将需要安装一些软件 - 最低限度的 Java 和 MySQL 以及一个 Servlet 容器,例如 Tomcat。您有两个选择,要么手动进行这些安装,要么使用Puppet 之类的工具来自动化它们——我推荐后者,因为一旦设置好,您可以在几分钟而不是几天内构建一个服务器。

Then you're going to need some sort of build process that's separate from your IDE. Current popular tools for this are Mavenor Gradle. The aim of the build is to produce an artifact containing everything that must be added to the server for your application to be installed. In the case of a stand-alone application this could be a zip with want amounts to a disk image in it (the jar(s), configuration, start/stop scripts) or in the case of a web-app a war, which is the standard deployment structure for a web-app.

然后,您将需要某种独立于 IDE 的构建过程。当前流行的工具是MavenGradle。构建的目的是生成一个工件,其中包含必须添加到服务器才能安装应用程序的所有内容。在独立应用程序的情况下,这可能是一个 zip 文件,其中包含一个磁盘映像(jar、配置、启动/停止脚本),或者在 Web 应用程序的情况下是一场战争,其中是 Web 应用程序的标准部署结构。

Next you're going to need some sort of release process for your application and database scripts. I'm most familiar with Maven that is capable of creating jars, wars, propriatary zips or packages and which, with the use of its Release Plugin, performs all the updates, VCS tagging and deployment (into a repository such as Nexus) needed to perform a release. The database is a bit more tricky and is normally dictated by the DBAs who will apply the database changes - typically you end up with the scripts needed to create and modify the database plus a master script that runs those scripts in the correct order. Pop those in a zip and into the repository for safe keeping too.

接下来,您将需要某种应用程序和数据库脚本的发布过程。我最熟悉 Maven,它能够创建 jars、wars、专有 zips 或包,并且使用它的Release Plugin执行所有更新、VCS 标记和部署(到诸如Nexus 之类的存储库中)执行释放。数据库有点棘手,通常由将应用数据库更改的 DBA 指定 - 通常,您最终会得到创建和修改数据库所需的脚本以及以正确顺序运行这些脚本的主脚本。将它们放入 zip 并放入存储库以安全保存。

Once you've released, you need some way to take the artifacts from the repository and install them onto the server. Obviously this can be done manually or with scripts but again I'd recommend using Puppet, at least for the artifacts targetted at the server. With the database scripts, while this can be automated too, it's more common to connect to the database and apply the scripts manually using a database console (don't forget to take backup of both code and database prior to installing a new version).

发布后,您需要某种方式从存储库中获取工件并将它们安装到服务器上。显然,这可以手动或使用脚本完成,但我再次建议使用Puppet,至少对于针对服务器的工件。使用数据库脚本,虽然这也可以自动化,但更常见的是连接到数据库并使用数据库控制台手动应用脚本(不要忘记在安装新版本之前备份代码和数据库)。

Finally you can start your system.

最后你可以启动你的系统。

回答by dbermudez

First you need to install the database in the server (accesing by ssh)

首先你需要在服务器中安装数据库(通过ssh访问)

Then you need to copy the jar on the server (sftp, scp...) and install a JVM on it (if there isn't installed yet -> you can check it executing java -v)

然后你需要复制服务器上的jar(sftp,scp...)并在上面安装一个JVM(如果还没有安装 - >你可以检查它执行java -v)

After that, like Brierson says, you need to run your jar (java -jar server.jar).

之后,就像 Brierson 所说的,你需要运行你的 jar (java -jar server.jar)。

It's possible you need to add some libraries needed by the jar (define the classpath, explained here)

您可能需要添加 jar 所需的一些库(定义类路径,在此处解释)

If your jar is a server then when executed probably it will bind to a port (for example, port 8080). To connect to your app in the remote server, you need to address your client to 122.152.12.33:8080 (IP:port)

如果您的 jar 是服务器,那么在执行时它可能会绑定到一个端口(例如,端口 8080)。要连接到远程服务器中的应用程序,您需要将客户端寻址到 122.152.12.33:8080 (IP:port)

Your server may have a firewall, so maybe you need to open the port (8080) of your server application.

您的服务器可能有防火墙,因此您可能需要打开服务器应用程序的端口 (8080)。