1

Hi I'm new to programming in PHP. I use a WAMP server -> Apache version 2.4.4 and PHP version 5.4.16

I want to execute java program from php script, here's my java program

   file : test.java
    import java.io.*; 
    public class test {
        public static void main(String args[]) {
            System.out.println("Hello World");
        }
    }

I used

exec("javac test.java");
exec(java test);

and it's no use. So then I tried to put the command code in "runfile.bat"

javac test.java > error.txt
java test > output.txt

when I execute runfile.bat by left clicking twice, it run perfectly and there's text "Hello World" in output.txt

then I try from PHP:

exec('start /B /C runfile.bat');

... and output.txt has nothing in it.

7
  • What folder do you run your program in? It looks like you place runfile.bat in the same folder. When you double click it, Windows automatically starts the file in the appropriate folder. When you execute the bat through exec or Task Scheduler you should add the working directory. Commented Apr 22, 2014 at 14:09
  • i placed the runfile.bat in the same folder as php Commented Apr 22, 2014 at 14:10
  • this is my full code $curdir = getcwd(); chdir('./Programs'); echo getcwd(); $result=shell_exec('cmd.exe /b /c runfile.bat'); echo $result; chdir($curdir); Commented Apr 22, 2014 at 14:10
  • Do you mind voting up my comment because it's the same as the answer below :) Commented Apr 22, 2014 at 14:13
  • Seems you are new to StackOverflow. Please accept the correct answer Commented Apr 22, 2014 at 14:28

2 Answers 2

1

Definitely PHP's exec() would do.

<?php
exec('java test', $output, $return_var);

// `$output` captures the output of command executed
print_r($output);

// Generally `$return_var` becomes 0 if the command was successful
if (0 == $return_var) {
    // Command successfull
}
?>

PS: Make sure your Java class-paths are correct. Always look for PHP Manual -- which the best ever manual I've ever seen.

Sign up to request clarification or add additional context in comments.

Comments

0

You probably want to fully-qualify paths here (especially if this is a web application):

exec("javac test.java");
exec("java test");

Something more like:

exec("/path/to/javac /and/the/path/to/test.java");
exec("/path/to/java /and/the/path/to/test");

It's possible that it's "running" but the working directory isn't correct, so maybe it can't find the javac and java executables or can find them but can't find test.java. When you run these things manually the executables are in your path and the file is in your working directory, but both of those may not be true for the PHP context.

Aside from that, it's also good practice to fully-qualify things anyway. It's more explicit and less prone to error (or even exploit, if a user were to somehow upload a javac executable of their own to your website they could execute it with elevated permissions using your script).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.