Results 1 to 12 of 12
  1. #1
    manou2324 is offline Member
    Join Date
    Mar 2013
    Posts
    56
    Rep Power
    0

    Default Execute .php through java

    Hi,

    i have a php program and another java program.I want to execute the .php through java :
    Java Code:
    public class Php {
    	public static void main (String [] args) throws Exception {	
         String url="http://localhost/projet.php";
    	try {
    	    URL myURL = new URL(url);
    	    URLConnection myURLConnection = myURL.openConnection();
    	   myURLConnection.connect();
    	} 
    	catch (MalformedURLException e) { 
    	    // new URL() failed
    		System.out.println("faileddddd");
    	} 
    	catch (IOException e1) {   
    	    // openConnection() failed
    	    System.out.println("failed");
    	}
    }
    }
    but it doesn't execute anything and also doesn't make any exception.
    I put the "projet.php" under the directory "C:\wamp\www"
    Please tel me if there is any mistake

    Thank you

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Execute .php through java

    I want to execute the .php
    .php are script files. That is they are just plain text files that are designed to be fed to some other program (a php processor) that will use them as input.

    The processor interprets the .php and the resulting output is used for some purpose: it might be sent by a server to a browser where it can be displayed as a web page. Or it might send its output to the console.

    Your program doesn't invoke a php processor, and, if you have a web server at localhost that does, you are doing nothing with the output it produces.

  3. #3
    manou2324 is offline Member
    Join Date
    Mar 2013
    Posts
    56
    Rep Power
    0

    Default Re: Execute .php through java

    i want to say that my php program "projet.php" takes as an input,a text file and produces 90 text files..
    this treatment must be done when i launch the "projet.php" through a java code

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Execute .php through java

    Check the logs of the server at localhost and make sure that it processed the php file (without error). It would also be a good idea to make the php script produce some output that the server can send back to your Java program for you to print as confirmation that all went well.

  5. #5
    manou2324 is offline Member
    Join Date
    Mar 2013
    Posts
    56
    Rep Power
    0

    Default Re: Execute .php through java

    But my php program : "projet.php" goes well when it is launched by the WampServer..It produces the files that i need.
    do you understand me?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Execute .php through java

    Get it to send something back to the client.
    That way you have something in your Java code to read.

    When you get the php to run on the server is it the same URL you use?
    http://localhost/projet.php
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    manou2324 is offline Member
    Join Date
    Mar 2013
    Posts
    56
    Rep Power
    0

    Default Re: Execute .php through java

    yes it is the same url :
    http://localhost/projet.php[/QUOTE]

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Execute .php through java

    And when you get it to respond in some way to the client?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    manou2324 is offline Member
    Join Date
    Mar 2013
    Posts
    56
    Rep Power
    0

    Default Re: Execute .php through java

    Quote Originally Posted by Tolls View Post
    And when you get it to respond in some way to the client?
    i didn't understand..
    I use just that way to run my program.I launch it from wampServer (navigator) by this url

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Execute .php through java

    Use URLConnection.getInputStream().
    Output the result to somewhere (even System.out would do).
    That way you can see what is happening.
    An error on the server will not be reflected in your Java side as an exception.
    It will appear in the stream as an http response type (eg 404, for page not found).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    manou2324 is offline Member
    Join Date
    Mar 2013
    Posts
    56
    Rep Power
    0

    Default Re: Execute .php through java

    I added just :
    Java Code:
    BufferedInputStream in = new BufferedInputStream(myURLConnection.getInputStream());
    it works thank you very much

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Execute .php through java

    I'm glad you've got it doing what you want.

    My guess (and I'm no expert) is that merely connecting is not enough to tell the server to actually process the .php script. You must actually create an input stream which, from the server's point of view means that it must get busy and run the .php script to produce the output (even if there isn't any).

Similar Threads

  1. Help! I got an error when I try to execute a java!
    By matiaz14 in forum New To Java
    Replies: 10
    Last Post: 03-11-2012, 10:17 PM
  2. Execute sql script from java
    By nikosdi in forum JDBC
    Replies: 0
    Last Post: 06-10-2011, 01:52 PM
  3. how to execute the Java program to 0 sec?
    By yanipra in forum New To Java
    Replies: 4
    Last Post: 03-01-2011, 09:33 AM
  4. execute a .exe with attributes from java
    By danielleon11 in forum Advanced Java
    Replies: 1
    Last Post: 07-14-2010, 01:25 AM
  5. time to execute a java app
    By svpranay in forum New To Java
    Replies: 6
    Last Post: 06-23-2010, 08:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •