简体   繁体   English

使用Php exec函数在Azure上运行jar文件

[英]Using Php exec funtion to run a jar file on Azure

I will give some background first. 我将首先介绍一些背景。 I have three files: test.php, test.txt, myJar.jar . 我有三个文件:test.php,test.txt,myJar.jar。 Here are the code inside each of them 这是每个里面的代码

test.php: test.php:

<?php
exec("java -cp myJar.jar Hello test.txt", $o);
print_r($o);
?>

the actual code that was used to make the myJar.jar file: 用于制作myJar.jar文件的实际代码:

import java.io.*;
public class Hello
{
    public static void main(String[] args) throws Exception
    {
        FileReader file = new FileReader(args[0]);
        BufferedReader reader = new BufferedReader(file);
        String line = reader.readLine();
        while (line != null)
        {
            System.out.println(line);
            line = reader.readLine();
        }
    }
}

test.txt: test.txt:

hello hello dinosir
this is a test
testing to the extreme
adding more crap here
and here

now I put these files in htdocs and run localhost/test.php using chrome I get: 现在我将这些文件放在htdocs中,并使用chrome运行localhost / test.php,我得到: 在此处输入图片说明

Perfect exactly what I wanted. 正是我想要的完美。 The problem is when I upload these files to github and link it to a website on azure, I go to the website but I only see Array(). 问题是当我将这些文件上传到github并将其链接到azure上的网站时,我转到该网站,但仅看到Array()。 I assume its because Azure doesnt have jre to run the "java -cp myJar.jar Hello test.txt" command. 我认为这是因为Azure没有jre来运行“ java -cp myJar.jar Hello test.txt”命令。 Any idea on how I could install/enable jre on azure to make the test.php run the same way as on my localhost 关于如何在azure上安装/启用jre以使test.php运行与在本地主机上相同的方式的任何想法

Have you checked your website configurations? 您是否检查过网站配置? Make sure that Java isn't set to "off" (the default) 确保未将Java设置为“关闭”(默认) 网站的Azure门户配置页面

The only way to make this work in Azure as PHP app is to create .bat file. 在Azure as PHP应用程序中使其工作的唯一方法是创建.bat文件。 Just like this: 像这样:

example.bat should contain folowing: example.bat应该包含以下内容:

set PATH=%PATH%;%JAVA_HOME%/bin
java -jar %~dp0java-file.jar %*

Its important to realize that the only thing you should change is java-file.jar and it needs to be wrapped with %~dp0 and %* . 重要的是要认识到,您唯一需要更改的是java-file.jar ,它需要用%~dp0%*包装。

Then execute .bat file with php : 然后用php执行.bat文件:

exec("example.bat", $output);

Thats it. 而已。 Works for me. 为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM