SpinningAround
November 24th, 2009, 08:06 PM
It's something weird with Java's way to run UNIX scripts, if I tell Java to run the script nothing happen, but if I run the script manually does it work. What on earth is wrong?
Script
ifconfig | grep -q ppp0
echo $? > VpnState.txt
exit 1
Java Code
public static boolean Check(){
String scriptPath = "/where/it/is";
int status=1; //forced to change to return true
shell(scriptPath); //jump of to the part that run the script
String status = FileInOut.VpnRead(); //read VpnState.txt
status=Integer.parseInt(status); //convert to an int
return (status==0); // return true or false
}
Shell Code
public static void shell(String path){
try{
Runtime comPrompt = Runtime.getRuntime();
comPrompt.exec(path);
Wait.oneSec();
}
catch (IOException e){
System.out.println("Script Error " + e.getMessage());
}
}
Read, don't think there is any errors got an other similar that is working fine
public static String VpnRead(){
File file = new File(vpnPath);
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
String read="fail";
try{
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
read=dis.readLine();
read=read.trim();
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
}
catch (FileNotFoundException e){
System.out.println(e.getMessage());
}
catch (IOException e){
System.out.println(e.getMessage());
}
return read;
}
Just in case, does this work?
while(Check())
//and
if(Vpn.Check())
Script
ifconfig | grep -q ppp0
echo $? > VpnState.txt
exit 1
Java Code
public static boolean Check(){
String scriptPath = "/where/it/is";
int status=1; //forced to change to return true
shell(scriptPath); //jump of to the part that run the script
String status = FileInOut.VpnRead(); //read VpnState.txt
status=Integer.parseInt(status); //convert to an int
return (status==0); // return true or false
}
Shell Code
public static void shell(String path){
try{
Runtime comPrompt = Runtime.getRuntime();
comPrompt.exec(path);
Wait.oneSec();
}
catch (IOException e){
System.out.println("Script Error " + e.getMessage());
}
}
Read, don't think there is any errors got an other similar that is working fine
public static String VpnRead(){
File file = new File(vpnPath);
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
String read="fail";
try{
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
read=dis.readLine();
read=read.trim();
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
}
catch (FileNotFoundException e){
System.out.println(e.getMessage());
}
catch (IOException e){
System.out.println(e.getMessage());
}
return read;
}
Just in case, does this work?
while(Check())
//and
if(Vpn.Check())