Executer un script shell par java
momocanada
Messages postés
10
Date d'inscription
Statut
Membre
Dernière intervention
-
quelqun -
quelqun -
Bonjour,
je veux executer un script shell par un programme java ,
ceci est mon programme java
import java.io.* ;
public class test1
{
private static Runtime runtime=Runtime.getRuntime();
private static Process test ;
public static void main (String[] args )
{
try {
test=runtime.exec("./my-script" ) ;
//int exitval=test.exitvalue();
//System.out.println( "resultat" + exitval);
}
catch (IOException ex)
{
System.out.println("echec du test");
}
}
}
et ceci est mon script
#!/bin/bash
# My first script
echo "test line" > test
tcpreplay --intf1=eth1 --intf2=eth2 --cachefile=trace-http.cache trace-http.pcap
Le probleme est que la premiere commande "echo" est execute alors que la deuxieme commande qui un commande pour rejouer un trafic donne ne s'execute pas , sachant que j'ai deja teste le script sur la ligne de commandes et ça marche
est-ce que quelqu'un peut m'aider
merci beaucoup
je veux executer un script shell par un programme java ,
ceci est mon programme java
import java.io.* ;
public class test1
{
private static Runtime runtime=Runtime.getRuntime();
private static Process test ;
public static void main (String[] args )
{
try {
test=runtime.exec("./my-script" ) ;
//int exitval=test.exitvalue();
//System.out.println( "resultat" + exitval);
}
catch (IOException ex)
{
System.out.println("echec du test");
}
}
}
et ceci est mon script
#!/bin/bash
# My first script
echo "test line" > test
tcpreplay --intf1=eth1 --intf2=eth2 --cachefile=trace-http.cache trace-http.pcap
Le probleme est que la premiere commande "echo" est execute alors que la deuxieme commande qui un commande pour rejouer un trafic donne ne s'execute pas , sachant que j'ai deja teste le script sur la ligne de commandes et ça marche
est-ce que quelqu'un peut m'aider
merci beaucoup
A voir également:
- Executer un script shell par java
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel - Télécharger - Jeux vidéo
- Classic shell - Télécharger - Personnalisation
- Script vidéo youtube - Guide
- Eclipse java - Télécharger - Langages
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");
}
}