2
public class the-file{
  public static void main(String[] args){
    System.out.println("Hello World");
  }
}
<?php
  $dir = "cd '/var/www/the-file/java/'";
  echo shell_exec("$dir && javac the-file.java && java the-file");
?>

I execute echo shell_exec("$dir && javac the-file.java && java the-file") but it does not show anything on my website. But if I do echo shell_exec("$dir && java the-file") or echo shell_exec("$dir && java the-file && javac the-file.java"), there is Hello World printed out on my website, is there a way to resolve this, I want it to compile echo shell_exec("$dir && javac the-file.java && java the-file") and output Hello World.

michelle
  • 49
  • 1
  • 6
  • 5
    One: You only need to compile a Java file once to run it many times. Two: Starting a Java process is slow, you are meant to do that once with a long running program to amortize the cost. Three: Running an external Java process from PHP is a terrible idea. Four: **Why** do you want to do this? – Elliott Frisch Nov 28 '21 at 17:04
  • Three: Why is it a terrible idea? Four: I am uploading a file to PHP and converting the file to pdf that requires a library in java. (The `Hello World` is just testing whether it works) – michelle Nov 28 '21 at 17:13
  • 1
    Because cgi-bin does not scale. We learned that in the 90s. You don't need to recompile the Java program to change command line arguments. Still shouldn't mix languages. Use Java, or use PHP. Or use both but on different ports. See [What is Java Servlet?](https://stackoverflow.com/q/7213541/2970947) – Elliott Frisch Nov 28 '21 at 17:17
  • Is there any way to retrieve the link from the server in java given that the link can change from time to time (since I'm uploading files and the link changes according to the file id generated) – michelle Nov 28 '21 at 17:40
  • 1
    This isn't necessarily an awful idea, as this code isn't being run for every single request, and users will not mind waiting a while for PDF conversion. I am guilty of doing similar things in the past, when it would be overly complicated using a single programming language. Still, you should create a full Java project, compile it into a JAR, and run it from PHP. This would require a lot of learning, but by then, you may just want to create the whole thing in Java. – TheKodeToad Nov 28 '21 at 17:41
  • For shell_exec, please use full paths :. For example change `javac the-file.java` to `/opt/jdk1.7.0_80/bin/javac /var/www/test1/the-file.java` (same for java the-file) – Ken Lee Nov 28 '21 at 17:47
  • @Ken Lee Why can't I just change directory and execute the file? – michelle Nov 28 '21 at 17:54
  • 1
    For PHP shell execution, environment variables (e.g. PATH) **may** not be effective because the "user" executing the script is apache (or nginx, etc.), not you. So in your case with two simple lines, please include the full paths – Ken Lee Nov 28 '21 at 17:55
  • @Ken Lee I see, thanks, let me try it out – michelle Nov 28 '21 at 18:02
  • 1
    Why not just do the PDF conversion using PHP? – ADyson Nov 28 '21 at 23:17

0 Answers0