如何通过 ssh 运行 Java 程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10064178/
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 Java programs over ssh?
提问by Sam Stern
Let's say I make a Java project in Eclipse that has 3-10 classes and one of which has a main(String[] args) method that starts the whole program and takes 4 arguments at the command line. Let's also say that this project has 6-10 .jar files in src/lib that it needs to run.
假设我在 Eclipse 中创建了一个 Java 项目,该项目有 3-10 个类,其中一个有一个 main(String[] args) 方法,该方法启动整个程序并在命令行接受 4 个参数。假设这个项目在 src/lib 中有 6-10 个 .jar 文件需要运行。
If I have ssh access to another computer (UNIX on both ends) and I want to run this program, how exactly do I go about doing so?
如果我可以通过 ssh 访问另一台计算机(两端都是 UNIX)并且我想运行这个程序,我到底该怎么做?
I ask because I have been doing some distributed computing projects and I need to run my program on multiple machines but I am a total command line noob and I don't have physical access to all of the machines.
我问是因为我一直在做一些分布式计算项目,我需要在多台机器上运行我的程序,但我是一个完全的命令行菜鸟,我没有对所有机器的物理访问权限。
Edit: Seems I need to SCP the files over. Can someone show me the particular command that makes the Java program run? Including what directory I should run it from and how to include the JAR dependencies.
编辑:似乎我需要对文件进行 SCP 处理。有人可以向我展示使 Java 程序运行的特定命令吗?包括我应该从哪个目录运行它以及如何包含 JAR 依赖项。
回答by Andrew
to run
跑步
java -jar thejarfile.jar "arg1" "arg2" "ectect.."
java -jar thejarfile.jar "arg1" "arg2" "ectect.."
if you want to run it in the background java -jar thejarfile.jar "arg1" "arg2" &
如果你想在后台运行它java -jar thejarfile.jar "arg1" "arg2" &
to kill it if its in the background ps -auxto get the id and then kill (id number)
杀死它,如果它在后台ps -aux获取 id 然后杀死(id 号)
回答by Adam Liss
In general, you use ssh to log into a remote computer, and then you run programs from that machine's storage, using that machine's resources.
通常,您使用 ssh 登录远程计算机,然后使用该计算机的资源运行该计算机存储中的程序。
So if you want to run your Java program on a different machine, you'd need to copy the required files there, then ssh to that machine and run the program from the remote command line.
因此,如果您想在另一台机器上运行您的 Java 程序,您需要将所需文件复制到那里,然后通过 ssh 连接到该机器并从远程命令行运行该程序。
回答by Andrew T Finnell
You write a tiny bootstrap program that will be your MicroKernel. You SCP that MicroKernel to the remote machine. This program will use a custom ClassLoader to pull the rest of the dependencies for your real application down into memory or some storage onto the remote machine. Look into the URL ClassLoader for some help as it can load JAR files from HTTP addresses.
您编写了一个微型引导程序,它将成为您的微内核。您将该 MicroKernel SCP 到远程机器。该程序将使用自定义类加载器将实际应用程序的其余依赖项拉入内存或远程机器上的某些存储中。查看 URL ClassLoader 以获得一些帮助,因为它可以从 HTTP 地址加载 JAR 文件。
Or you can just zip up your whole program, SCP it to the remote machine, unzip it then run 'java' like normal. If you have SSH access you should have access to scp files onto that machine. If not you can always SSH into the remove machine then scp
them from your machine.
或者你可以压缩你的整个程序,将它 SCP 到远程机器,解压缩它然后像往常一样运行“java”。如果您有 SSH 访问权限,您应该可以访问该机器上的 scp 文件。如果没有,您始终可以通过 SSH 连接到删除机器,然后scp
从您的机器中删除它们。
Example:
例子:
ssh myname@myremotemachine
> mkdir /location/to/program
> scp myname@mydevmachine:/location/to/program/* /location/to/program
This all works really well if you have your SSH keys setup properly and don't have to put in a password.
如果您正确设置了 SSH 密钥并且不必输入密码,那么这一切都非常有效。