Questions about .jar files and running a java program through PHP


I'm building a website which will use files in order to render social graphs. These files are created by a backend in JAVA. This JAVA program consists of 4 classes and 4 libraries. I know that in order to run a JAVA program through PHP, I need to call it with the "exec" command but all the examples I saw have .jar executables and not .java files in the syntax. So, I'd like to ask two questions:

  1. Is it possible to call just the .java main class from PHP without creating a .jar file? And if so, what's the syntax of the "exec" command?
  2. If I have to create the .jar executable, would I just have to place it in the website folder and just call it? I mean, does the .jar file contain all the classes and libraries that the program needs? I don't talk about JAVA libraries, I have 4 specific libraries (MongoDB, Neo4j, GEXF parser and Lucene DB) which are not part of Java defaults. Thanks in advance and please forgive my syntax and spelling mistakes.



You have to at a minimum compile the .java files into .class files. It would be easier if they are in a jar file, but that is not required.

1) a) java -cp /location/to/.class/files

b) java -jar /location/to/the.jar

2) the jar file does not have to be executable, though if that makes it easier, set the manifest up correctly and there you go.

When you say libraries, you mean other .jar files or native .dll or .so libraries? If the later, you have to have your library path setup to find them.




There is a PHP-Java bridge project on SourceForge. Also, you could expose your back end process by running it as a servlet, which is probably the proper way of doing it. If all else fails you can just "exec" it.

People are also looking for solutions to the problem: php - retrning an associative array from mysqli statement result


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.