簡體   English   中英

將查詢從php傳遞到Java

[英]Passing query from php to java

我試圖從URL獲取一些查詢,然后將它們傳遞給Java程序以進一步執行。 我面臨的問題是我的PHP代碼正在調用我的Java程序,但未傳遞值。 到現在為止,我一直在研究這些代碼,

PHP程式:

<?php
$phonecode= $_GET['phonecode'];
$keyword= $_GET['keyword'];
$location= $_GET['location'];
exec("C:\Users\Abc\Documents\NetBeansProjects\JavaApplication11\src\javaapplication11\main.java -jar jar/name.jar hello" . $phonecode . ' ' . $keyword . ' ' . $location, $output);
print_r($output);
?>

JAVA程序:

public class Main
{

    public static void main(String args[])
    {
        try
        {
            String phonecode = args[];
            System.out.println(args[]);
            System.out.println(phonecode);// i have only tried to print phonecode for now
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
} 

好的,您發布的Java代碼有幾個問題,這是您發布的內容的有效版本:

class Main
{
    public static void main(String[] args)//String[] args, not String args[]
    {
        if (args.length == 0)
        {//check to see if we received arguments
            System.out.println("No arguments");
            return;
        }
        if ( args.length < 3)
        {//and make sure that there are enough args to continue
            System.out.println("To few arguments");
            return;
        }
        try
        {//this try-catch block can be left out
            String phonecode = args[0];//first arg
            String keyword = args[1];//second
            String location = args[2];//third
            //print out the values
            System.out.print("Phonecode: ");
            System.out.println(phonecode);
            System.out.print("keyword: ");
            System.out.println(keyword);
            System.out.print("location: ");
            System.out.println(location);
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());//get the exception MESSAGE
        }
    }
}

現在,將其另存為.java文件並進行編譯 ,它應該會產生一個Main.class文件。 我是從命令行編譯的:

javac main.java

我沒有安裝netbeans,但是我懷疑.class文件將被寫入另一個目錄,例如:

C:\Users\Abc\Documents\NetBeansProjects\JavaApplication11\bin\javaapplication11\Main.class
                                                     // note the BIN

然后,要執行該命令,您需要運行java命令,並將其路徑傳遞到此Main.class文件,而Main.class .class擴展名。 因此,我們最終得到:

java /path/to/Main 123 keywrd loc

應產生輸出:

Phonecode: 123
keyword: keywrd
location: loc

在您的PHP代碼中:

exec('java /path/to/Main '.escapeshellarg($phonecode). ' '.escapeshellarg($keyword).' '.escapeshellarg($location), $output, $status);
if ($status === 0)
{//always check exit code, 0 indicates success
    var_dump($output);
}
else
    exit('Error: java exec failed: '.$status);

還有其他一些問題:例如$phonecode = $_GET['phonecode']; 不檢查$_GET參數是否存在。 如果沒有,您的代碼將發出通知。 修理:

$phonecode = isset($_GET['phonecode']) ? $_GET['phonecode'] : '';

其他小問題包括:反斜杠是字符串中的特殊字符,用於轉義序列中: \\n是換行符。 PHP甚至可以在Windows上處理* NIX目錄分隔符/ 使用它,或轉義反斜杠( C:\\\\Users\\\\Abc\\\\等等)。
包含PHP代碼的文件不需要關閉?>標記。 實際上: 建議您不要使用它

您的Java代碼應如下所示

 public static void main (String[] args) {
   for (String s: args) {
     System.out.println(s);
   }
 }

注意String[] args ,而不是String args[]

同樣在exec的PHP端,如果您希望將字符串看成2個單獨的參數,則需要在字符串hello和變量$ phonecode之間使用空格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM