2016-04-19 9 views
1

Ich habe JDK und ich versuche, Main.java Programm ausführen, die Endlosschleife enthält und ich möchte Java Main.java < input.txt> output.txt Befehl, wenn es zu brechen geht in Endlosschleife und wenn nicht Endlosschleife als nicht wird nicht Programm zu brechen .. Jede Lösung? In SchwierigkeitenBreak Endlosschleife Java-Programm mit PHP-Exec-Befehl

<?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 
} 

?> 
+0

Sie wollen eine Lösung? Lass es nicht ins Unendliche gehen. Eine andere Lösung wäre, den Prozess zu beenden, wenn Sie die Prozess-ID kennen. –

Antwort

0

Sie benötigen einen Port zu öffnen, um in Java zu hören, und dann verbinden und etwas an dieser Port von PHP zu senden.

Check out @TomaszNurkiewicz here beantworten, wo er sagt

""“

Was möchten Sie wahrscheinlich ist es, ein Serversocket und hören auf, es zu schaffen:

ServerSocket serverSocket = new ServerSocket(4000); 
Socket socket = serverSocket.accept(); 

Die zweite Zeile wird blockiert, bis eine andere Software an Port 4000 angeschlossen wird. Dann können Sie von dem zurückgegebenen Socket lesen. Sehen Sie sich dieses Tutorial an, das ist eigentlich ein sehr weites Thema (th Lesen, Protokolle ...)

"" "

und zum Öffnen der Buchse mit php bis Sie den Code (oder somethign nahe an sie verwenden können) zur Verfügung gestellt here von @sanmi von the manual

" ""

<?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); 
?> 

""“

1

Versuchen sie es in einer Klasse wie diese Umhüllung (im wesentlichen inEinwickeln 210, um die PID zu bekommen und damit im Hintergrund zu arbeiten)

<?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; 
    } 
} 
?> 
+0

Wenn das funktioniert, ist es die beste Antwort. – kpie

+0

funktioniert das in Windows? –

+0

Und wie kann ich mein Programm mit Process ausführen? –

1

Dies ist nur ein Vorschlag. Es wird eine bessere Antwort für Ihre Frage geben.

Innerhalb der Java-Infinity-Schleife überprüfen Sie einen Wert aus einer anderen Textdatei. Es ist wie

while true { 

v = readFile('your_txt_file.txt') 
if v == "true" { 
    break; 
} 
//do your stuff } 

Wenn Sie your_txt_file.txt Wert auf false, was jemals außer wahr dann java Schleife wird Pause einstellen.