3

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 .

I have tested using a sample java program like

PHP Script:

<?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>&1");
 $OUTPUT = shell_exec("java  HelloWorld 2>&1");
 echo $OUTPUT;
 ?>

I have placed the java files with in "C:\xampp\htdocs\java_bridge\" if I execute the file through PHP the result is executed .

HelloWorld.java:

public class HelloWorld {
public static void main(String[] args) {
    // Prints "Hello, World" to the terminal window.
    System.out.println("Hello, World");
}
}

Output :

Hello, World

ExtractPagesFromPdfAndSaveAsNewPDFPage.java :

    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<PDPage> 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();
    }
}
}

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("");

My application path is "C:\xampp\htdocs\java_bridge\JavaApplication1\src";

My class file path is "C:\xampp\htdocs\java_bridge\JavaApplication1\build\classes";

If I run the

shell_exec("javac  \JavaApplication1\src\ExtractPagesFromPdfAndSaveAsNewPDFPage.java  2>&1");
$OUTPUT = shell_exec("java \JavaApplication1\build\classes\ExtractPagesFromPdfAndSaveAsNewPDFPage 2>&1");

It returns the output as Error :

Error: Could not find or load main class \JavaApplication1\build\classes\ExtractPagesFromPdfAndSaveAsNewPDFPage

Please any one let me know how to execute an java application thru php script .

Thanks in advance .

500rs Dreams
  • 33
  • 1
  • 3
  • Verify the path of your java class. Why do not put the full java class path? – thepaulo Dec 15 '16 at 10:25
  • @Sonor : I have given the full path for class shell_exec("javac C:\\xampp\\htdocs\\java_bridge\\JavaApplication1\\src\ExtractPagesFromPdfAndSaveAsNewPDFPage.java 2>&1"); $OUTPUT = shell_exec("java C:\\xampp\\htdocs\\java_bridge\\JavaApplication1\\build\\classes\\ExtractPagesFromPdfAndSaveAsNewPDFPage 2>&1"); echo $OUTPUT; but still the same problem occurs – 500rs Dreams Dec 15 '16 at 10:33

1 Answers1

0

You can also use the exec method of PHP

For compiling:

<?php exec("javac C:\fakepath\file.java", $output); ?>

For executing:

<?php exec("java C:\fakepath\file arguments", $output); ?>

For executing a jar file:

<?php exec("java -jar C:\fakepath\file.jar arguments", $output); ?>
thepaulo
  • 370
  • 4
  • 10