When I run my application and scan for an IP it freezes for some reason. Also, I am not sure if the Yum method is exiting once it run the command, or if it lives forever. My target was to make something like $ ./inLinuxRunMe &, where it runs in the background, and when the job is done, it kills itself.
I don’t think that my Yum method is doing this, because it freezes when I start heavy loads such as playing video’s etc.
public class MyShell
{
public static String whatIsMyIP = null;
public static void main(String args[])
{
t = new javax.swing.Timer(10000, new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
/* Scanner: if the ip change, show the new on too. */
whatIsMyIP = MyShell.Yum("ifconfig eth0 | grep inet").toLowerCase());
/* Some logical conditions ... such as if ran then dont run, run once etc..*/
bootMe();
}
});
t.start();
/* Launch: Main application server, runs forever as server */
// MyShell.Yum("java -cp myjar.jar launch.MainApplicationAsServer");
}
/* Boot loader */
public static void bootMe() throws IOException
{
MyShell.Yum("java -cp myjar.jar launch.MainApplicationAsServer");
}
/* Question: How to optimize it? So that,
it execute the shell command and quite*/
public static String Yum(String cmds)
{
String value = "";
try
{
String cmd[] = {"/bin/sh", "-c", cmds };
Process p=Runtime.getRuntime().exec(cmd);
BufferedReader reader=new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line=reader.readLine();
while(line!=null)
{
value += line ;
line=reader.readLine();
}
p.waitFor();
} catch(IOException e1) {
} catch(InterruptedException e2) {
}
return value;
}
}
From the Java API:
So it repeats every 10 seconds. You need to tell it not to repeat:
So: