如何在服务器上运行java程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2350529/
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
How to run a java program on a server?
提问by Yatendra Goel
I have made a java application that stores data from a .csv file to a MySql database. Now my client want it to upload this application to his web space (web space he has taken for his website) so that he can run that program on that server.
我制作了一个将数据从 .csv 文件存储到 MySql 数据库的 Java 应用程序。现在我的客户希望它将此应用程序上传到他的网络空间(他为他的网站占用的网络空间),以便他可以在该服务器上运行该程序。
I have used FileZilla
software to upload the program to his web hosting but now I don't know how to run that program on his server.
我已经使用FileZilla
软件将程序上传到他的虚拟主机,但现在我不知道如何在他的服务器上运行该程序。
To run it on localsystem, it needs to open a command-prompt window to run it.
要在本地系统上运行它,它需要打开一个命令提示符窗口来运行它。
Is there any specific feature that the web-hosting must support to run that java program?
网络托管必须支持运行该 Java 程序的任何特定功能吗?
As it stores data from a file (.csv file) to a MySql database, then would it better to deploy that program on the server on which the database is being hosted instead of the server on which the website is being hosted?
由于它将数据从文件(.csv 文件)存储到 MySql 数据库,那么将该程序部署在托管数据库的服务器上而不是托管网站的服务器上会更好吗?
采纳答案by seas
Indeed, saying "web-application", we usually mean a special application, programmed to run on the web-server all the time, just waiting for requests from user to process.
事实上,说“网络应用程序”,我们通常指的是一种特殊的应用程序,它被编程为一直在网络服务器上运行,只是等待用户的请求来处理。
In your case, you have a console-based application.
就您而言,您有一个基于控制台的应用程序。
Depending on the configuration of the server, no of these applications could be run on your client web-hosting, any of them or both.
根据服务器的配置,这些应用程序中的任何一个都不能在您的客户端网络托管上运行,也不能在其中任何一个或两者上运行。
Since, usually web-hosting is provided by hosting company, they may have configurations ready for running your applications, may have it turnable on/off or even take money for this.
因为,通常网络托管是由托管公司提供的,他们可能已经准备好运行您的应用程序的配置,可能会打开/关闭它,甚至为此赚钱。
In case of internal company server, you need to ask your customer and its IT-stuff to configure this.
如果是公司内部服务器,您需要询问您的客户及其 IT 人员进行配置。
Finally, you'll need to ask: 1. Does server support SSH? - it's simply a remote console. Usually it's running at port 22 and you many check it with command "telnet yourserver22" (windows and linux) - if it doesn't reject your connect - means SSH is configured. 2. Does your server have java installed and if it is available for your account via SSH connection?
最后,您需要问: 1. 服务器是否支持 SSH?- 它只是一个远程控制台。通常它在端口 22 上运行,您可以使用命令“telnet yourserver22”(windows 和 linux)检查它- 如果它没有拒绝您的连接 - 意味着配置了 SSH。2. 您的服务器是否安装了 java,是否可以通过 SSH 连接用于您的帐户?
- Only if your customer really mean web-application instead of console based application you need to ask if server has web-application server for Java - usually, it's something like Apache Tomcat, Jetty, JBoss, Weblogic, etc. But this way will require application modification in order to run it in web-server.
- 仅当您的客户真正指的是 Web 应用程序而不是基于控制台的应用程序时,您才需要询问服务器是否具有用于 Java 的 Web 应用程序服务器 - 通常,它类似于 Apache Tomcat、Jetty、JBoss、Weblogic 等。但这种方式将需要应用程序修改以便在网络服务器中运行它。
If you will decide to use console-application and not "upgrade" it to web-application, you really may run it at the host your database is running (again, you'll need SSH). You'll save time on remote database access operations - theoretically, your program will work faster.
如果您决定使用控制台应用程序而不是将其“升级”为 Web 应用程序,则您确实可以在运行数据库的主机上运行它(同样,您将需要 SSH)。您将节省远程数据库访问操作的时间——理论上,您的程序会运行得更快。
回答by cherouvim
Your application is most likely a console application and not a web based one.
您的应用程序很可能是一个控制台应用程序,而不是基于 Web 的应用程序。
Your client will need to SSH to the server and do something like:
您的客户端需要通过 SSH 连接到服务器并执行以下操作:
java com.foobar.FooBar
or:
或者:
java -jar FooBar.jar
回答by Jon Skeet
Having "web space" doesn't necessarily mean he has sufficient access to the server to run arbitrary programs (or a MySql database).
拥有“网络空间”并不一定意味着他有足够的权限访问服务器来运行任意程序(或 MySql 数据库)。
You couldpotentially rewrite the app as a web application, but that may well not be a good fit.
您可能会将该应用程序重写为 Web 应用程序,但这可能不太合适。
The first thing to check is whether he canlog in to run programs from a console of some description. If he can, that's fine - and the way to go. If he can't, you'll need to think about why he really wants it to be on his web server, and whether it makes sense to run the app there - or whether it would be better to do it elsewhere.
首先要检查的是他是否可以从某种描述的控制台登录以运行程序。如果他可以,那很好 - 也是要走的路。如果他不能,您需要考虑为什么他真的希望它在他的网络服务器上,以及在那里运行应用程序是否有意义 - 或者在其他地方做它是否更好。
回答by DCC
If your webhost has java available you could try to execute it from a php cronjob without the need for ssh:
如果您的网络主机有 java 可用,您可以尝试从 php cronjob 执行它,而无需 ssh:
<?php system('java -jar /path/to/your/program.jar'); ?>
Cron jobs are easily done from cpanel.(P.S. the above is the code for the php file it is not what you put in the cron page, you put the php -f /path/to/php/file.php in the cron page)
Cron 作业很容易从 cpanel 完成。(PS 以上是 php 文件的代码,它不是你放在 cron 页面中的代码,你把 php -f /path/to/php/file.php 放在 cron 页面中)