I want to connect a remote machine(has a Linux operation system) from my Java Program and I want to run a script on it. I will process the result of that script(it writes something to console) too.
What do you suggest for me? If anything needed I can explain more.
There are SSH libraries in java. you can use them to execute scripts on a remote machine.
Related
I'm developing a java application from a ubuntu machine and I want to reach a remote file that resides on a ubuntu machine, I want a java code that makes me able to rename or delete this file. I have no idea about how to achieve this task.
Anyone can help me and thanks.
To go about this, first of you have to break the problem into two parts.
First part is connecting to the remote computer. So you will have to look for a better way to connect to a remote computer with java. For this there are several ways to go about it.
You can google it to get a head start. And then come back here when you get stuck
Also you can check out the responses in this link. They will help you out
how to connect remote windows machine by java?
The second phase is doing IO (input output) operations after a successful connection.
Here is a link for sample code with JsCh
http://www.jcraft.com/jsch/examples/Shell.java.html
I have a use case where in I have to connect to a CLI and execute commands in that CLI using java. Usually without using java, I do it by opening a linux terminal and connecting to other CLI and execute commands there. I have to implement the same using Java. I am able to run the commands on the linux terminal using Runtime.getRuntime().exec(). But, I need some help in executing the commands after connecting to a particular CLI from linux terminal using Java
I think you are asking how to make use of pseudo-terminals. I was going to warn you this was likely more complicated than you were bargaining for, but a short search showed the pty4j library which may be of help.
When you run an external program using Runtime.exec(), it returns a Process object that you can use to interact with the running process. That object has a getOutputStream() method that you can use to send commands to the process, and, getInputStream() and getErrorStream() methods that you can use to read messages produced by the process.
I have an EC2 instance and i have a .jar that i already took (wget script command) from S3.
I want to run this .jar file, but not on start-up, just when i decide.
I'm using Java with "Eclipse Juno" with help from AWS SDK.
Running a user data script doesn't help me here.
Can some one help me with this?
Thanks in advance.
What you are looking for is unix cronjobs,
take a look at the cron job manual here and set it to run whenever you want.
also, you can install WEBMIN on your system, it gives you a great server management including cronjobs and scheduled commands as you need via web interface
if you want to run something with java, just use
String[] command={"java","-jar","myfile.jar"};
Runtime.getRuntime().exec(command);
You should try using JSch for remote invocation of shell scripts it's basically SSH within Java.
And here's a simple example.
I have a little question: we have to run Java programs and parts of the code will be uploaded by the users.
So I want to know what's the best way to run them? I know 2 possible ways,
exec("javac Usercode.class") and then run the whole thing with exec("java Main"), but I tried it with exec() and it don't work. maybe because the http is not root? But I don't know exactly why.
http://php-java-bridge.sourceforge.net/pjb/ ?
Any suggestions?
And another question is, how can I run these programs in a sandbox. we have a Debian server and so it's no problem to execute the command with a limited time, but is there a possible way to run the whole code in a sandbox?
Ideas for sandboxing:
Run in a chroot using e.g. Debian's schroot command. Protects against them accessing files outside of the chroot but not against them doing things like opening sockets etc.
Each user has their own Linux username against which they validate. Commands will then be run under the appropriate username (e.g. by using sudo or a set-uid executable).
Maintain a pool of virtual servers - expensive and complicated but gives best isolation.
I want to facilate my client to run java program through UNIX command prompt using some shells. It'll look more effecient if they would be able to give input through some GUI. So it can be tested immedietely. I dont want prefer unix commands fro input.
Can somebody tell me how to run Java swing or applet programs in UNIX?
As Thompson mentioned, looking at Java Web Start could be a good idea.
Otherwise, if what you want is to execute, using a *NIX-like terminal, an application located on a remote host and have it rendered on your local display, then you need to do a few things:
you need a working X server on the local machine
you need to export the DISPLAY to the local machine (you can do this by setting up the DISPLAY environment variable on the remote system)
then you need to start your Java app from the command-line.
Hope this helps.
Here's an example of how to export your display over SSH.
Java programs use the X windows system (just like any other GUI on Unix). Assuming your X windows system is setup correctly, you should just open up a JFrame and do your GUI coding just like Windows.
Using the command prompt to launch a GUI is so last millennium. If you can distribute from a server, look into Java Web Start to provide the end-user with a simple and painless install.
Oh, and of course, follow Starkey's advice to throw a JFrame into the mix.
If you have an X-server installed locally, Putty can tunnel the X11-graphics generated by Linux Java back from the server to your local machine, and view it there.
If the above doesn't make sense to you, your next best bet is either running the Java code locally with Java Web Start (and code it to communicate back to the remote server) or run Servlets inside a Java Web Server running on the remote host.
In other words, GUI over a Putty connection is not something which is easily done.