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.

4
  • please try the accepted solution in stackoverflow.com/questions/278868/… and then report back. Commented Jan 19, 2013 at 13:42
  • 1
    You don't point to the .class file. Just to the directory that contains the class. Commented Jan 19, 2013 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. Commented Jan 19, 2013 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. Commented Jan 19, 2013 at 13:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.