我有JDK,我正在尝试执行包含无限循环的Main.java程序,我想破坏java Main.java<input.txt>output.txt命令,如果它进入无限循环,如果不是无限循环,则不会破坏程序。。有解决方案吗??出现故障
<?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
}
?>
试着用这样的类包装它(本质上是用nohup COMMAND > /dev/null 2>&1 & echo $!
包装它以获得pid并在后台使用它)
<?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;
}
}
?>
这只是一个建议。你的问题会有更好的答案。
在java无穷大循环中,检查其他文本文件中的一些值。它就像
while true {
v = readFile('your_txt_file.txt')
if v == "true" {
break;
}
//do your stuff }
如果您可以将your_txt_file.txt值设置为false,而不是true,那么java循环将中断。
您需要在java中打开一个要侦听的端口,然后从php连接并发送一些东西到该端口。
查看@TomaszNurkiewicz在他说的地方的回答
""
您可能想要创建一个ServerSocket并监听它:
ServerSocket serverSocket = new ServerSocket(4000);
Socket socket = serverSocket.accept();
第二行将被阻塞,直到其他软件连接到您的计算机端口4000。然后您可以从返回的套接字中进行读取。看看这个教程,这实际上是一个非常广泛的主题(线程、协议…)
""
为了用php打开套接字,您可以使用@sanmi从手册中提供的代码(或接近它的somethign)
""
<?php
error_reporting(E_ALL);
/* Get the port for the WWW service. */
$service_port = getservbyname('www', 'tcp');
/* Get the IP address for the target host. */
$address = gethostbyname('www.example.com');
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " .
socket_strerror(socket_last_error()) . "'n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
echo "socket_connect() failed.'nReason: ($result) " .
socket_strerror(socket_last_error($socket)) . "'n";
}
$in = "HEAD / HTTP/1.1'r'n";
$in .= "Host: www.example.com'r'n";
$in .= "Connection: Close'r'n'r'n";
$out = '';
echo "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
echo "OK.'n";
echo "Reading response:'n'n";
while ($out = socket_read($socket, 2048)) {
echo $out;
}
socket_close($socket);
?>
""