使用PHP exec命令中断无限循环java程序
我有JDK,我正在尝试执行Main.java程序,该程序包含无限循环,如果它进入无限循环,我想中断java Main.java使用PHP exec命令中断无限循环java程序,java,php,Java,Php,我有JDK,我正在尝试执行Main.java程序,该程序包含无限循环,如果它进入无限循环,我想中断java Main.javaoutput.txt命令,如果不是无限循环,则不中断程序。。有什么解决办法吗??陷入困境 <?php exec('cmd /k c:/wamp/www/javac Main.java 2>&1', $outputAndErrors, $return_value); for($i=0 ; $i<sizeof($outputAndErrors) ;
<?php
exec('cmd /k c:/wamp/www/javac Main.java 2>&1', $outputAndErrors, $return_value);
for($i=0 ; $i<sizeof($outputAndErrors) ; $i++)
{
$output1=htmlspecialchars($outputAndErrors[$i],ENT_QUOTES);
echo "$output1";
$flag=1;
}
if(!$flag)
{
exec('cmd /k c:/wamp/www/java Main.java < input.txt > output.txt', $outputAndErrors, $return_value);
//want to give timeout but if exec goes to infinite loop than below statement will not executed
}
?>
您需要在java中打开一个要侦听的端口,然后从php连接并向该端口发送内容 查看@TomaszNurkiewicz他说的答案 “”“ 您可能需要创建一个ServerSocket并监听它:
ServerSocket serverSocket = new ServerSocket(4000);
Socket socket = serverSocket.accept();
第二行将阻塞,直到其他软件在端口4000连接到您的计算机。然后您可以从返回的套接字读取。请看本教程,这实际上是一个非常广泛的主题(线程、协议…)
“”“
要用php打开套接字,您可以使用@sanmi提供的代码(或与之相近的代码)
“”“
“”“尝试将其包装在这样的类中(基本上是将其包装在
nohup命令>/dev/null 2>&1&echo$!
中,以获取pid并在后台以这种方式使用它)
这只是一个建议。你的问题会有更好的答案 在java infinity循环中,检查其他文本文件中的一些值。就像
while true {
v = readFile('your_txt_file.txt')
if v == "true" {
break;
}
//do your stuff }
如果您可以将您的\u txt\u file.txt值设置为除true之外的任何值的false,那么java循环将被中断。您想要解决方案吗?不要让它进入无限。另一个解决方案是,如果您知道进程ID,那么就终止进程。以及如何使用进程运行我的程序?不,这在Windows上不起作用。要在Windows上执行类似的操作并将其放在后台,您可以尝试类似于
pclose(popen(“start/B”。$cmd,“r”)
的操作,要使用它,您只需对新进程运行命令,并在($process.status()){//kill if timed out}或类似操作时设置循环。请参见代码段顶部的示例
<?php
// You may use status(), start(), and stop(). notice that start() method gets called automatically one time.
$process = new Process('ls -al');
// or if you got the pid, however here only the status() metod will work.
$process = new Process();
$process.setPid(my_pid);
?>
<?php
// Then you can start/stop/ check status of the job.
$process.stop();
$process.start();
if ($process.status()){
echo "The process is currently running";
}else{
echo "The process is not running.";
}
?>
<?php
/* An easy way to keep in track of external processes.
* Ever wanted to execute a process in php, but you still wanted to have somewhat controll of the process ? Well.. This is a way of doing it.
* @compability: Linux only. (Windows does not work).
* @author: Peec
*/
class Process{
private $pid;
private $command;
public function __construct($cl=false){
if ($cl != false){
$this->command = $cl;
$this->runCom();
}
}
private function runCom(){
$command = 'nohup '.$this->command.' > /dev/null 2>&1 & echo $!';
exec($command ,$op);
$this->pid = (int)$op[0];
}
public function setPid($pid){
$this->pid = $pid;
}
public function getPid(){
return $this->pid;
}
public function status(){
$command = 'ps -p '.$this->pid;
exec($command,$op);
if (!isset($op[1]))return false;
else return true;
}
public function start(){
if ($this->command != '')$this->runCom();
else return true;
}
public function stop(){
$command = 'kill '.$this->pid;
exec($command);
if ($this->status() == false)return true;
else return false;
}
}
?>
while true {
v = readFile('your_txt_file.txt')
if v == "true" {
break;
}
//do your stuff }