title: Execute a Java Application from Php
body: <p>Hi friends I have to execute a java application from php script , for that I have moved the jre folder files to xammp folder and set the path to the folder . </p>
  
  <p>I have tested using a sample java program like </p>
  
  <p>PHP Script: </p>
  
  <pre><code>&lt;?php
   $JAVA_HOME = "\jre1.8.0_60";
   $PATH = "$JAVA_HOME/bin:".getenv('PATH');
   putenv("JAVA_HOME=$JAVA_HOME");
   putenv("PATH=$PATH");
   //enter rest of the code here
   shell_exec("java  HelloWorld.java  2&gt;&amp;1");
   $OUTPUT = shell_exec("java  HelloWorld 2&gt;&amp;1");
   echo $OUTPUT;
   ?&gt;
  </code></pre>
  
  <p>I have placed the java files with in "C:\xampp\htdocs\java_bridge\" if I execute the file through PHP the result is executed .</p>
  
  <p>HelloWorld.java: </p>
  
  <pre><code>public class HelloWorld {
  public static void main(String[] args) {
      // Prints "Hello, World" to the terminal window.
      System.out.println("Hello, World");
  }
  }
  </code></pre>
  
  <p>Output : </p>
  
  <p><strong>Hello, World</strong></p>
  
  <p>ExtractPagesFromPdfAndSaveAsNewPDFPage.java : </p>
  
  <pre><code>    import java.io.File;
      import java.util.List;
      import org.apache.pdfbox.pdmodel.PDDocument;
      import org.apache.pdfbox.pdmodel.PDPage;
  
      /**
       * 
       * @author udaykiran.pulipati
       *
       */
  
      @SuppressWarnings("unchecked")
      public class ExtractPagesFromPdfAndSaveAsNewPDFPage {
          public static void main(String[] args) {
              try {
  
      //            String sourceDir = "C:\\Users\\user1\\Desktop\\java_pdf_split\\scan.pdf";
      String sourceDir = "C:\\Users\\user1\\Desktop\\java_pdf_split\\scan.pdf";
          String destinationDir = "C:/PDFCopy/";
          File oldFile = new File(sourceDir);
  
          String fileName = oldFile.getName().replace(".pdf", "");
          System.out.println("New Filename: "  + fileName);
          System.out.println("Old Filename: "  + oldFile.getName());
          if (oldFile.exists()) {
              File newFile = new File(destinationDir);
              if (!newFile.exists()) {
                  newFile.mkdir();
          } 
  
          PDDocument document = PDDocument.load(sourceDir);
          List&lt;PDPage&gt; list = document.getDocumentCatalog().getAllPages();
  
          int pageNumber = 1;
          for (PDPage page : list) {
              PDDocument newDocument = new PDDocument();
              newDocument.addPage(page);
  
              newFile = new File(destinationDir + fileName + "_"+ pageNumber +".pdf");
              newFile.createNewFile();
  
              newDocument.save(newFile);
              newDocument.close();
              pageNumber++;
              }
          } else {
              System.err.println(fileName +" File not exists");
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  }
  </code></pre>
  
  <p>But now I have to execute a java application name Java application1 with in that I have a java file named "ExtractPagesFromPdfAndSaveAsNewPDFPage" if I have to execute this , How I shoud give the exec path(""); </p>
  
  <p>My application path is "C:\xampp\htdocs\java_bridge\JavaApplication1\src";</p>
  
  <p>My class file path is "C:\xampp\htdocs\java_bridge\JavaApplication1\build\classes";</p>
  
  <p>If I run the     </p>
  
  <pre><code>shell_exec("javac  \JavaApplication1\src\ExtractPagesFromPdfAndSaveAsNewPDFPage.java  2&gt;&amp;1");
  $OUTPUT = shell_exec("java \JavaApplication1\build\classes\ExtractPagesFromPdfAndSaveAsNewPDFPage 2&gt;&amp;1");
  </code></pre>
  
  <p>It returns the output as 
  Error :</p>
  
  <p><strong>Error: Could not find or load main class \JavaApplication1\build\classes\ExtractPagesFromPdfAndSaveAsNewPDFPage</strong></p>
  
  <p>Please any one let me know how to execute an java application thru php script .</p>
  
  <p>Thanks in advance .</p>