Pouya Fx Pouya Fx - 5 months ago 28
Java Question

execute java from php from different directory

I have linux server(Debian). I have java program(compiled) in directory (reza). I am trying to execute this java program from /var/www and through php script.

$com=shell_exec('java /reza/z');


When call in above format it dosent return any results or execute java program.
When I call one php script in reza folder with same call format it returns correct response:

$com=shell_exec('php /reza/a.php');


Any idea how to make this to work correctlly?

P.S. when call java from same directory from php it returns correct response:

$com=shell_exec('java z');


Java file is compiled and include class file too.

Java sample code is:

import java.io.*;

public class z{

public static void main(String args[])throws IOException{

File file = new File("Hello1.txt");
// creates the file
file.createNewFile();
// creates a FileWriter Object
FileWriter writer = new FileWriter(file);
// Writes the content to the file
writer.write("This\n is\n an\n example\n");
writer.flush();
writer.close();

//Creates a FileReader Object
FileReader fr = new FileReader(file);
char [] a = new char[50];
fr.read(a); // reads the content to the array
for(char c : a)
System.out.print(c); //prints the characters one by one
fr.close();
}
}


Regards,

Answer

Use the classpath switch:

$com=shell_exec('java -cp /reza z');