0

I'm trying to connect a Java project with a website. The project is located in "website_folder/data/backend" and at that time, I want to test if the following example works:

[java code]

public static void main(string args[]) {
   if(args[0].equals("5") {
      File f = new File("/location/to/file/result.txt");
      if(!f.exists()) {
         f.createNewFile();
         FileWriter fstream = new FileWriter("/location/to/file/result.txt");
         BufferedWriter out = new BufferedWriter(fstream);
         out.write("It works!");
         out.close();
      }
   }
}

And in the PHP file I have to following command:

<?php
exec("java -cp /data/backend/Project/build/classes/project/Main.class/ main 5",$output); 
?>

but nothing happens! I mean, the file isn't created in the directory I have specified in the main method. I have tried to run the .jar file of the project in the PHP command, to pass the argument "5" via a variable, to add to the directory path -in the exec command- this: "i:/xampp/htdocs/website_folder/" (I:\ is the disk drive where I have my virtual server -xampp- installed), but in vain. Am I doing something wrong with the syntax of the command?

Edit: I changed the command to point the .jar file (java -jar /data/backend/Project/dist/Project.jar 5) and the problem is solved.

Lefteris008
  • 899
  • 3
  • 10
  • 29
  • please try the accepted solution in http://stackoverflow.com/questions/278868/calling-java-from-php-exec?rq=1 and then report back. – Gordon Jan 19 '13 at 13:42
  • 1
    You don't point to the .class file. Just to the directory that contains the class. – adarshr Jan 19 '13 at 13:44
  • As adarshr has said, you could have just tried this out to see if it worked. It should tell you `class main not found`. – Overv Jan 19 '13 at 13:45
  • I've tried both solutions (export DYLD_LIBRARY_PATH=""; and to point to the .class folder) but nothing happens. I'm using Windows 8. Does that mean that I have to specify something before the 'java -cp'? I mean the location of java or something? In some examples I see that some people add '/usr/bin/java' which definitely has to do with Linux. – Lefteris008 Jan 19 '13 at 13:54

1 Answers1

0

Try this:

"java -cp /data/backend/Project/build/classes/ project.Main 5"

This assumes, that the class which contains your main method is in package project and is called Main. If this is not the case, adapt accordingly.

Henry
  • 42,982
  • 7
  • 68
  • 84