Run the JAR file in PHP and write the file

I have the following problem in php when excecuting a jar file. I use the following command:

exec("java -jar JavaProject4.jar"; 

JavaProject4.jar creates the txt file in the path specified in the java code.

When I run the project in NetBeans, a txt file is created. However, when I excecute jar in php, I get no errors, but I can not get the file.

Here is the java code that I use to write the file:

 public static void main(String[] args) throws InterruptedException, FileNotFoundException, IOException { Main a = new Main(); List<Double> l1 = new ArrayList<Double>(); l1 = a.compute_features(); //System.out.println(l1); FileWriter fstream1 = new FileWriter("C:/wamp/www/test/out.txt"); BufferedWriter out1 = new BufferedWriter(fstream1); out1.write(l1.toString()); out1.newLine(); out1.close(); } 

Im using wamp server with php 5.2.4 and latest java version.

Thanks a lot!

EDIT: The problem is resolved, I moved the main java file in NetBeans to the default package, and also fixed the wrong path, and now everything works as expected.

Thank you all

+4
source share
2 answers

When you run it with PHP, how do you do it? Are you using the PHP CLI (command line interface), or are you using it through the Apache module (CGI or otherwise)? The reason I'm asking is because the problem you are facing may have something to do with the user who is executing the script. If you use the CLI, you work as a Windows user, however, if you use it through Apache, then it works like any Apache user works like. Therefore, you may need to grant the appropriate permissions to the Apache user for this directory that you are writing.

Regards, Ralfe

+1
source

I think you need to specify the full path for: - the java executable is your bank

Example:

exec ("/ usr / bin / java -jar / my / java / project / path / JavaProject4.jar");

0
source

Source: https://habr.com/ru/post/1392216/


All Articles