Executer un script shell par java
Fermé
momocanada
Messages postés
10
Date d'inscription
vendredi 25 avril 2008
Statut
Membre
Dernière intervention
9 juin 2008
-
4 juin 2008 à 17:53
quelqun - 9 juin 2008 à 17:17
quelqun - 9 juin 2008 à 17:17
A voir également:
- Executer un script shell par java
- Script vidéo youtube - Guide
- Waptrick java football - Télécharger - Jeux vidéo
- Java apk - Télécharger - Langages
- Jeux java itel football - Télécharger - Jeux vidéo
- Classic shell windows 10 français - Télécharger - Personnalisation
1 réponse
Voici une solution qui devrait marcher. Tu sauvegardes cette classe dans le même rep que ton shell et tu lances ...
Bien sûr, tu peux donner tout le chemin absolu de ton script dans ton main java. Bonne chance.
import java.io.*;
public class ExecDemoShell {
static void lancerShell (String nomShell)
throws IOException {
// start the ls command running
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("ksh" + " " + nomShell);
// put a BufferedReader on the ls output
InputStream inputstream =
proc.getInputStream();
InputStreamReader inputstreamreader =
new InputStreamReader(inputstream);
BufferedReader bufferedreader =
new BufferedReader(inputstreamreader);
// read the ls output
String line;
while ((line = bufferedreader.readLine())
!= null) {
System.out.println(line);
}
// check for ls failure
try {
if (proc.waitFor() != 0) {
System.err.println("exit value = " +
proc.exitValue());
}
}
catch (InterruptedException e) {
System.err.println(e);
}
}
public static void main(String[] args)
throws IOException {
if (args.length != 1) {
System.out.println(
"missing directory");
System.exit(1);
}
lancerShell("my-script");
}
}
Bien sûr, tu peux donner tout le chemin absolu de ton script dans ton main java. Bonne chance.
import java.io.*;
public class ExecDemoShell {
static void lancerShell (String nomShell)
throws IOException {
// start the ls command running
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("ksh" + " " + nomShell);
// put a BufferedReader on the ls output
InputStream inputstream =
proc.getInputStream();
InputStreamReader inputstreamreader =
new InputStreamReader(inputstream);
BufferedReader bufferedreader =
new BufferedReader(inputstreamreader);
// read the ls output
String line;
while ((line = bufferedreader.readLine())
!= null) {
System.out.println(line);
}
// check for ls failure
try {
if (proc.waitFor() != 0) {
System.err.println("exit value = " +
proc.exitValue());
}
}
catch (InterruptedException e) {
System.err.println(e);
}
}
public static void main(String[] args)
throws IOException {
if (args.length != 1) {
System.out.println(
"missing directory");
System.exit(1);
}
lancerShell("my-script");
}
}