Java Linux skript aus java starten

honky-tonk

Commodore Pro
🎅Rätsel-Elite ’24
Registriert
Feb. 2009
Beiträge
4.579
Hey,
wie der titel schon sagt, ich möchte mit meinem java programm ein script im linux terminal starten. und das so, dass der user sieht was das terminal grade macht.

mit Java kann ich schon programme starten:
Runtime.getRuntime.exec("programm");

wenn ich jetzt allerdings statt programm mein script wähle öffnet sich der editor und mein script wird angezeigt. :-/

man bräuchte halt am besten sowas wie "terminal -./script" was dem terminal einen befehl übergibt.

Jemand ne idee wie man das realisieren kann?

Danke schonmal!
 
x-terminal-emulator -e "/pfad/zum/script"

script:
Code:
#!/bin/bash
echo "Hallo Welt! :)"

# verhindert das Schließen des Terminals
exec bash
 
Ich könnte mir zwei banale Gründe vorstellen, die du am Anfang mal überprüfen kannst. Zum einen, hast du deinen Skript ausführbar gemacht und zum zweiten hast du bei exec "./" das vor den Pfad zum Skript gemacht? Ach ja, hast dus auch schon mit absoluter Pfadangabe versucht oder bisher nur mit relativer?

Ich weiß, eigentlich wirklich banal, aber das sind so Standardfehler, die mir andauernd unterlaufen^^
 
import java.io.InputStream;
...
String cmd = "ls -l"; // this is the command to execute in the Unix shell
// create a process for the shell
ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
pb.redirectErrorStream(true); // use this to capture messages sent to stderr
Process shell = pb.start();
InputStream shellIn = shell.getInputStream(); // this captures the output from the command
int shellExitStatus = shell.waitFor(); // wait for the shell to finish and get the return code
// at this point you can process the output issued by the command
// for instance, this reads the output and writes it to System.out:
int c;
while ((c = shellIn.read()) != -1) {System.out.write(c);}
// close the stream
try {shellIn.close();} catch (IOException ignoreMe) {}
 
also vielen dank erstmal,
x-terminal-emulator funktioniert gut!

das mit dem process builder ist ja schon etwas komplexer...werd ich mir ggf. später nochmal reinziehen falls das prog noch fancy werden soll ^^

Danke!
 
Zurück
Oben