简体   繁体   中英

How do i launch a GUI Terminal and run commands on it in java

I have a program which runs when you double click it. What i want to do is:

Double click a jar File

在此处输入图像描述
Launch some GUI 在此处输入图像描述 And Run Commands in the terminal through the java program

在此处输入图像描述

(just an example, this is not what i want to do)

I Tried this:

public class Main{
    public static void main(String [] args){
        Runtime.getRuntime().exec("/bin/bash sudo SOME_COMMAND_HERE");
    }
}

( SOME_COMMAND_HERE is just a replacement)

It didn't launch a terminal.

(i have the GUI part so no need to write code for that)

So how do i do this? I have been spending a past day or two for just finding answers on stack overflow, and intense googling. Plus the Reason why i need it is because i want to show the user some progress and entering the password when running sudo commands.

Can some Please help? Thanks in advance.

In your example the process might get stuck as you are not reading it's stdout and stderr streams. From the Process documentation :

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the process may cause the process to block, or even deadlock.

So essentially you are just asking how to execute a process from Java. It is irrelevant that to the user your program would display a terminal-like UI. Knowing this we could rephrase your question and find answers like

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM