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 .