Jäpp. Du vill alltså bara köra ditt PHP-script via ett Java-program på den lokala datorn?
public class RunPHP {
public static void main( String[] args ) {
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec( "C:\\PHP\\php.exe C:\\sökväg\\till\\fil.php" );
/* För att se vad som genereras, så läser du av så här: */
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( process.getInputStream() ) );
String str = "";
while( ( str = bufferedReader.readLine() ) != null )
System.out.println( str );
bufferedReader.close();
} catch( Exception e ) {
System.err.println( "Felmeddelande: " + e.getMessage() );
}
}
}
public class RunPHP {
public static void main( String[] args ) {
try {
Runtime runtime = Runtime.getRuntime();
try
{
while(true)
{
Process process = runtime.exec( "C:\\PHP\\php.exe C:\\sökväg\\till\\fil.php" );
/* För att se vad som genereras, så läser du av så här: */
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( process.getInputStream() ) );
String str = "";
while( ( str = bufferedReader.readLine() ) != null )
System.out.println( str );
bufferedReader.close();
//Vänta 20 minuter och kör sedan koden igen
Thread.sleep(1200000);
}
}
catch(InterruptedException ie)
{
}
} catch( Exception e ) {
System.err.println( "Felmeddelande: " + e.getMessage() );
}
}
}
Annars kan du köra med crontab om du kör på en linux server eller shemalagda aktiviteter eller vad det nu heter i Windows. Då startar programmet vid vissa intervall.
Tack, programmet fungerar utmärkt. Hur gör jag för att starta det på en server? Måste jag vara administratör för servern eller finns det något sätt att göra det från klienten om man har tillgång till katalogerna?
Den sista klassen som Spin hade vänligheten att utveckla får jag inte att fungera, förmodligen för att jag har en brandvägg installerad. Det jag saknar är metoder att se om uppkopplingen fungerar och vad som är fel om den inte gör det.
public abstract void connect()
throws IOExceptionOpens a communications link to the resource referenced by this URL, if such a connection has not already been established.
If the connect method is called when the connection has already been opened (indicated by the connected field having the value true), the call is ignored.
URLConnection objects go through two phases: first they are created, then they are connected. After being created, and before being connected, various options can be specified (e.g., doInput and UseCaches). After connecting, it is an error to try to set them. Operations that depend on being connected, like getContentLength, will implicitly perform the connection, if necessary.
Throws:
IOException - if an I/O error occurs while opening the connection.
See Also:
connected
273 ms totalt · 4 externa anrop · v20260731065814-full.1dc6f849