How to run Java programs over ssh? - java

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.
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?
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.

to run
java -jar thejarfile.jar "arg1" "arg2" "ectect.."
if you want to run it in the background java -jar thejarfile.jar "arg1" "arg2" &
to kill it if its in the background ps -aux to get the id and then kill (id number)

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.
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.

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.
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.
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.

Related

run a jarfile in a different machine from an sqlserver/issue a terminal command from an sqlserver to another windows machine

I have a series of stored procedures(say, SP1, SP2, SP3) consecutively running on an sqlserver(say, serverA).
After SP3 finishes execution, I want to run a jar file in some machine (say, computerA). Is there a way for me to trigger the run of my jar file in computerA after SP3 finishes executing in serverA?
Note: I can only run my jar file in computerA because my jar file actually retrieves data from Bloomberg API. So, this jar file needs to run in a machine that has Bloomberg terminal on it. Only computerA has a Bloomberg terminal.
I think another way of saying this is how to issue a terminal command from an sqlserver to another windows machine. Because I can just issue the following command to computerA but I really don't know how to make that possible.
java -jar D:\Runnables\myJavaApp.jar

How to open java program in other linux computer without terminal holding?

I have written a java program with jar file. The java program is to update status of linux server so it need to keep running, but the linux server is in data center, so I need to remote to server to open the program. I use ssh to login linux server. Use command of "java -jar file.jar" to run the program.
However, the java program of the linux server will close if I close the terminal in my computer. Since I cannot keep opening my computer, I wanna know how to open the java programming without holding my computer terminal.
you need to use nohup to keep the program running after you log out.:
server:~name$> nohup java -jar file.jar &
this will keep your program running
Two ways
One
nohup java -jar file.jar &
Another
java -jar file.jar &
In both cases your process will go in background however the process will terminate in the second approach when shell terminates in second case.
If this program is intended to be running on all your machines for monitoring purposes, you should be running it as a service from your server's init system (systemd for most systems these days). You can use the Java Service Wrapper or jsvc or write your own init script.
Another solution apart from the proposed one:
screen -d -m java -jar your.jar
You will then have a detached screen with your java command in it. List with screen -l, reattach with screen -D -RR <screenid_obtained_via_screen_-ls>

How to Run Commands through a already running Java?

I was wondering if it was possible to execute commands from PHP to a Java prompt which is already running?
I have tried the solution listed here:
How to run a shell command through PHP code?
and this provided no functionality
Let me explain
The java is running on one screen of the linux server
sudo apt-get install screen
and running the .jar file through the command line.
I am then running a webserver, which will have an admin accessibility to restricted areas, which will contain scrips to run specific commands through that already running .jar file?
You can implement some kind of IPC. The java file listens to a port and receives the commands. Or you can write the commands in a specific file which the java programm reads. I think under linux you can also use shared memory: http://www.php.net/manual/en/book.shmop.php
It is possible by sending the command to the screen session. I used this for a minecraft server once.
screen -S <sessionname> -X stuff "<command>\r"
This would (IIRC) provide the same output as if you where inside the screen, typed the command and pressed enter.
I hope this was what you wanted.

using javaw to run jars in batch files results in more than one java processes in process explorer - XYNTService

I have a somewhat strange issue. I have a java application that installs few services that run as Jars. Previously I used installed Java to run these Jars. There are four services and all will be instantiated from a single batch file with sequential call to each other. Something like this,
start %JAVA_HOME% commandtoruntjarfile
would work and all four services will run in the background and only one java.exe visible in process explorer. So I had another service installed as windows service which would start stop these services by calling the run bat or shutdown bat.
Now the customer requirement changed to using an internalized version of java. I extract java to a location, make our own environment variable name "ABC_HOME" and the required syntax in batch changes to
%ABC_HOME%\javaw commandtorunjarfile
When its run it works. but there is no stopping these. When I go to process explorer I see 4 java.exe running each for the four run commands in the batch file. If I stop the windows service all the four keep working. If I restart the windows service the number of java.exe in process explorer goes to eight and keeps going up until windows has had enough of it.
How do I get around it? I think the solution should be to have the one java process in process explorer but I cant seem to find any solution for that.
[EDIT]
The four sub services are actually XYNT processes. In the normal scenario it would be something like this
[Process1]
CommandLine = java -Xrs -DasService=yes -cp jarfiles
WorkingDir = c: bin scache
PauseStart = 1000
PauseEnd = 1000
UserInterface = No
Restart = Yes
For using java from a specific location the following change was needed
CommandLine = %JAVA_PATH%\bin\java.exe -Xrs -DasService=yes -cp jarfiles
but this wouldn't work as it would not accept the path variable in XYNT.ini file. so I called a batch file here and in that batch file I used the above code. So here is what the change looks like,
CommandLine = batchfile.bat
and in batchfile.bat
%JAVA_PATH%\bin\java.exe -Xrs -DasService=yes -cp jarfiles
Usually, every Java program run on your system has its own virtual machine running, which means: one java.exe/javaw.exe per instance of your program.
I can not tell why it "worked" from your point of view with java.exe like you described first, but the behaviour you described for javaw.exe (having 4 java processes in the process explorer) would be what I'd have expected.
For me the question is not why you're seeing 4 vs. 1 java processes, but how you can start/stop the "services". Killing the Java VM externally doesn't seem a very good solution. I'd consider building some IPC into the Java services that allow you to gracefully terminate the processes.

Using RCP or FTP to copy files from a remote unix machine onto a local windows machine

I'm trying to write a piece of code that uses a ProcessBuilder to transfer a file on a remote UNIX machine onto the local Windows machine. On a brief bit of research I've found that either RCP or FTP should be a suitable thing to use.
Having done some research on the RCP command, I found instructions for copying files from a UNIX to windows machine, but they don't seem to work. The command I was told to use was:
rcp -r unixhost.user:/example/directory C:\Directory
However using this told me that C: was not a host. I tried it with the IP address, localhost, the alias of the windows pc in the hosts file but none of these worked, it either said permission denied or it could not connect to the host. Having looked up ftp it seems that would be another viable option. I'm not sure if I can execute a command using ProcessBuilder to successfully achieve this via FTP.
Would rcp or ftp be more suitable for this task? And how would I go about using them?
EDIT : To clarify, the script/batch file will be running on the Windows machine and pulling the files from the UNIX machine to windows.
It may be possible to escape the colon in the destination part. Have you tried quoting the destination?
rcp -r unixhost.user:/example/directory "C:\Directory"
It's been a while since I've done any command-line stuff on windows, but I remember the backslash character always being problematic. You may need to use forward slashes in the destination, since the rcp command is consuming the command line. You may also be able to use the backslash as an escape character, so you might try the following:
rcp -r unixhost.user:/example/directory C\:/Directory
If that won't work, you can explicitly set the current drive letter before calling the rcp command. If you're using a batch file, try the following two lines:
c:
rcp -r unixhost.user:/example/directory \Directory

Categories

Resources