0

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

Double click a jar File

enter image description here
Launch some GUI enter image description here And Run Commands in the terminal through the java program

enter image description here

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

Rishon_JR
  • 160
  • 11
  • 1
    It's not Java that controls the terminal. There are broadly two ways of running a java app: A. open terminal and enter commands and B. double-click an app to start. Now, it's down to your OS and desktop environment as to whether you see a terminal in scenario B. It's not up to Java and it knows nothing about it. Now, if you get your terminal, the Java code you describe is non-trivial. If you want to run arbitrary commands and app *via* Java (I'm not sure why you wouldn't do that directly at the terminal) then it's actually quite tricky to ensure that it always work. Depends on what's run – g00se Nov 26 '22 at 15:31
  • 1
    Perhaps this question will be useful for MacOS: https://superuser.com/q/174576 – Ben Anderson Nov 26 '22 at 16:06

1 Answers1

1

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

Queeg
  • 7,748
  • 1
  • 16
  • 42