使用简单的 Runtime.getRuntime().exec(cmd) 时代码冻结
当我运行应用程序并扫描 IP 时,它由于某种原因冻结。另外,我不确定 Yum 方法在运行命令后是否会退出,或者它是否会永远存在。我的目标是制作类似 $ ./inLinuxRunMe &
的东西,它在后台运行,当工作完成时,它会自行终止。
我不认为我的 Yum 方法正在执行此操作,因为当我开始重负载(例如播放视频等)时它会冻结。
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;
}
}
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在单独的线程中运行
Yum()
方法。Run you
Yum()
method in a separate thread.来自 Java API:
所以它每 10 秒重复一次。你需要告诉它不要重复:
所以:
From the Java API:
So it repeats every 10 seconds. You need to tell it not to repeat:
So: