简体   繁体   中英

Execute a Java Application from Php

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 .

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); ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM