How to send terminal commands [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to send terminal commands to the machine running the jar file, i know in some other languages like shell and python i think its something like this
run("cmd here")
Thanks
Also, i have read some previous posts here and they all seem to be for the windows command line.
I have not tried anything else yet as i do not know where to start, one thing i tried was using Jsch but that ended up being a giant program and i think its kind of unsatisfying when you need to input your machines details over and over again, even so most machines do not come pre equipped with SSH.

If you only want to execute the command, use
Runtime.getRuntime.exec(command);
where command is the command you want to execute as String.
If you want to read/write from/to stdin/stdout/stderr, you can use the returned Processes methods getOutputStram(), getInputStream() and getErrorStream().
Note that getInputStream() and getErrorStream() are for stdout and stderr while getOutputStream() is for stdin.

Related

Python - Best way to integrate Java GUI and python code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to make a desktop GUI that interacts with the user's files (with permission of course). My code to download files and put it in the user's choice of directory is all written in python. There is much more to this code, but everything is written in python.
I want the GUI part to be a desktop app, so the client can easily interact and function the program.
I want to make this GUI in java and have it interact with the python code once someone presses a button.
What are the best approaches to make this happen?
I would do it like this:
Process p = Runtime.getRuntime().exec("python app.py");
You can also pass arguments into the python program as command line arguments, like this:
Process p = Runtime.getRuntime().exec("python app.py arg1 arg2");
You might be interested in jython
http://www.jython.org
Also, you might think about running Python using JNI - this way, you will be able to keep the state of Python process while still being inside Java:
https://github.com/mkopsnc/keplerhacks/tree/master/python
Everything heavily depends on what you really want to achieve.

Java interact with windows cmd [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I've searched for similar questions, but despite there where a lot with a similar title all have asked something different.
In my case I want that my java program interact with the terminal (Windows) by writing command and reading the response.
I've found the class ProcessBuilder, but does not seem that do what I want
What I' m searching for is something so:
start the cmd at some given position (like C:\Users\federico)
issue a command (dir or cd desktop); this should not open a command prompt window
read any output that command may result in
and so on, up to the user's exit from the program.
I suspect you simply don't know the English terms for this: what you're looking for is a way to execute commands in the OS, which is called "exec" in every programming language I know of, including Java: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String)

How to make a remote shell gui in java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm working on a java program that needs to be able to connect to a remote server and execute system commands. How would I create a command shell that looked exactly the same as windows command prompt for the client, but executes the commands on the remote server? Thx in advance
It's possible to do, but it's not easy - java has never been great for command-line operation. If you want to do it, look for java-based SSH implementations.
On the other hand, if the user will be running the java program from the command-line, it may be possible to execute an ssh command on the same terminal.
Here is a page that describes executing an external program from java: http://alvinalexander.com/java/java-exec-processbuilder-process-1.

javan installation using python script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I’m new to python scripting and I need to install Java using python script . please suggest a method to do this.
Thanks all.
If i am not wrong you are looking for silent installation of Java. Here is a link that explains the same:
Silent installation of Java
Next you would want to run this command using python. For this you can use subprocess in python. Below is a link explaining it:
Using subprocess in Python
You want to display error stream too, so use the subprocess.Popen as:
process = subprocess.Popen(['command plus args as in above link'],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
out contains the output of the above command and err contains the error stream.
I would suggest you to install java from command line first, and then use this command in python script.
To install mySql or even tcl, follow similar steps,
1. Find how to install using command line
2. Execute the same command using python
Also, there may already be existing packages to do your job, if so you can use them.

Run cmd command without creating a .bat file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
is there a way to execute a cmd command like "move FolderA FolderB" without creating a .bat file and start it?
It would be nice if it would work without creating files on HDD.
Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","move","dirA/a.txt","dirB"});
Process process = new ProcessBuilder("cmd.exe",
"/c","move","dirA/a.txt","dirB").start();
ProcessBuilder is preferred to Runtime.exec() since Java 1.5, according to JavaDoc.
Be sure to read the Process Javadoc to understand how to read from and write to processes.
Shelling out for commands like move is bad practice, because it's neither portable nor secure. Work with File classes instead. But sometimes you have to shell out to interact with more esoteric external programs.

Categories