Issue
So I need to run a Debian(.deb) file at some point in my JavaFX program and so I tried using the code below on Linux
Runtime.getRuntime().exec("sudo dpkg -i "+pathToResource.getValue().toString());
but as you already know that requires me to also pass in the password along somehow for this to work
on Windows I used this sample code here
Runtime.getRuntime().exec(pathToResource.getValue().toString());
and it whenever administrator privilleges are needed then the software will just request them from the user and everything will run smoothly
But this doesn't seem to work on Linux, it just goes silent and nothing happens,
I already have an alternative to try to use TerminalFx, but if there's Anyone who knows what an alternative to run the .deb file or any other alternative, I'll be grateful. Thanks in Advance.
Solution
So I ended up using shellScript
Here's What I Did
- Requested the device password from the user through a simple JavaFX stage with just a text field and a submit button.
- Then I used the password that I just got from the user to create a shell script(Bash)
Here's my sample code to do create the sample shell Script
File shell_file = new File("path_to_the_file_where_the_shell_file_will_be_created_at_example_install.sh");
String variableName = "#!/bin/bash\npassword='" + password_from_JavaFx_stage + "'\n" +
"echo $password | sudo -S dpkg -i " + path_to_file_to_be_installed;
shell_file.createNewFile()// you can put an if to check if the function worked and otherwise do some other actions
FileWriter myWriter = new
FileWriter("path_to_the_file_where_the_shell_file_will_be_created_at_example_install.sh");
myWriter.write(messageContent);
myWriter.close();
Then Just run the command using the Processbuilder here's is a sample code to do so
String[] command = {String.valueOf(shell_file)};
ProcessBuilder processBuilder = new ProcessBuilder().command(command);
try {
Process process = processBuilder.start();
//read the output
InputStreamReader inputStreamReader = new
InputStreamReader(process.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String output = null;
while ((output = bufferedReader.readLine()) != null) {
System.out.println(output);
}
process.waitFor();
//close the resources
bufferedReader.close();
process.destroy();
}catch(IOException | InterruptedException e) {
e.printStackTrace();
}
So That's the idea and it's my best hope this will save somebody's time and helps out in one way or another.
Answered By - Fredy Nazary Answer Checked By - Katrina (WPSolving Volunteer)