Java interact with windows cmd [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 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)

Related

How to send terminal commands [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 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.

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.

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.

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.

Passing java results to command line [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
I was thinking of passing the result from Java run from command line into a command line variable. But after searching much for it, I only came across Runtime class which just runs the command.
Therefore, my question, is there anything I can do to get Java result into command line.
Platform of course is Windows.
There are only 2 ways any program (Java or otherwise) can send results back to the "command line":
1) exit code
2) capture the strings output by the program.
#1 can be done in Java using System.exit (or see a couple more options here).
#2 is done by the shell. In Unix, you typically use backticks or $():
OUTPUT=`java app.class`
In Windows Powershell, do something like
$result = & java app.class 2>&1 | Out-String
Possible duplicate of
how-do-i-get-the-result-of-a-command-in-a-variable-in-windows
FOR /F "delims=" %i IN ('java Main') DO set today=%i
Also allows today variable to be set.

Categories