将查询从php传递到java
我正在尝试从url获取一些查询,然后将它们传递给java程序以供进一步执行。我面临的问题是,我的php代码正在调用我的java程序,但没有传递值。 到目前为止,我一直在研究这些代码 PHP程序:将查询从php传递到java,java,php,Java,Php,我正在尝试从url获取一些查询,然后将它们传递给java程序以供进一步执行。我面临的问题是,我的php代码正在调用我的java程序,但没有传递值。 到目前为止,我一直在研究这些代码 PHP程序: <?php $phonecode= $_GET['phonecode']; $keyword= $_GET['keyword']; $location= $_GET['location']; exec("C:\Users\Abc\Documents\NetBeansProjects\JavaApp
<?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 static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
注意String[]args
,而不是String args[]
另外,在exec的PHP端,如果希望字符串hello和变量$phonecode被视为两个独立的参数,则需要在字符串hello和变量$phonecode之间留有空格。您的java代码应该如下所示
public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
注意String[]args
,而不是String args[]
另外,在exec的PHP端,如果希望字符串hello和变量$phonecode被视为两个独立的参数,则需要在字符串hello和变量$phonecode之间留有空格。您的java代码应该如下所示
public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
注意String[]args
,而不是String args[]
另外,在exec的PHP端,如果希望字符串hello和变量$phonecode被视为两个独立的参数,则需要在字符串hello和变量$phonecode之间留有空格。您的java代码应该如下所示
public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
注意String[]args
,而不是String args[]
另外,在exec的PHP端,如果希望将字符串hello和变量$phonecode视为两个独立的参数,则需要在字符串hello和变量$phonecode之间留出空间。好的,您发布的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
}
}
}
我没有安装netbeans,但我怀疑.class
文件将写入其他目录,例如:
C:\Users\Abc\Documents\NetBeansProjects\JavaApplication11\bin\javaapplication11\Main.class
// note the BIN
然后,要执行,需要运行java
命令,并将路径传递给该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=$\u GET['phonecode']
不检查$\u GET
参数是否存在。如果没有,代码将发出通知。要修复:
$phonecode = isset($_GET['phonecode']) ? $_GET['phonecode'] : '';
其他细节包括:反斜杠是字符串中的特殊字符,它用于转义序列:\n
是换行字符。PHP甚至可以在windows上处理*NIX目录分隔符/
。使用该选项,或取消反斜杠(C:\\Users\\Abc\\
等)。仅包含PHP代码的文件不需要关闭标记
?>
。事实上:。好的,您发布的Java代码有几个问题,下面是您发布的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
}
}
}
我没有安装netbeans,但我怀疑.class
文件将写入其他目录,例如:
C:\Users\Abc\Documents\NetBeansProjects\JavaApplication11\bin\javaapplication11\Main.class
// note the BIN
然后,要执行,需要运行java
命令,并将路径传递给该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=$\u GET['phonecode']
不检查$\u GET
参数是否存在。如果没有,代码将发出通知。要修复:
$phonecode = isset($_GET['phonecode']) ? $_GET['phonecode'] : '';
其他细节包括:反斜杠是字符串中的特殊字符,它用于转义序列:\n
是换行字符。PHP甚至可以在windows上处理*NIX目录分隔符/
。使用该选项,或取消反斜杠(C:\\Users\\Abc\\
等)。仅包含PHP代码的文件不需要关闭标记
?>
。事实上:。好的,您发布的Java代码有几个问题,下面是您发布的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
}
}
}
我没有安装netbeans,但我怀疑.class
文件将写入其他目录,例如:
C:\Users\Abc\Documents\NetBeansProjects\JavaApplication11\bin\javaapplication11\Main.class
// note the BIN
然后,要执行,需要运行java
命令,并将路径传递给该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=$\u GET['phonecode']
不检查$\u GET
参数是否存在。如果没有,代码将发出通知。要修复:
$phonecode = isset($_GET['phonecode']) ? $_GET['phonecode'] : '';
其他细节包括:反斜杠是字符串中的特殊字符,它用于转义序列:\n
是换行字符。PHP甚至可以在windows上处理*NIX目录分隔符/
。使用该选项,或取消反斜杠(C:\\Users\\Abc\\
等)。仅包含PHP代码的文件不需要关闭标记
?>
。事实上:。好的,您发布的Java代码有几个问题,下面是您发布的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
}
}
}
我没有安装netbeans,但我怀疑.class
文件将写入其他目录,例如:
C:\Users\Abc\Documents\NetBeansProjects\JavaApplication11\bin\javaapplication11\Main.class
// note the BIN
然后,要执行,需要运行java
命令,并将路径传递给该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=$\u GET['phonecode']
不检查$\u GET
参数是否存在。如果没有,代码将发出通知。要修复:
$phonecode = isset($_GET['phonecode']) ? $_GET['phonecode'] : '';
其他细节包括:反斜杠是字符串中的特殊字符,它用于转义序列:\n
是换行字符。PHP甚至可以在windows上处理*NIX目录分隔符/
。使用该选项,或取消反斜杠(C:\\Users\\Abc\\
等)。仅包含PHP代码的文件不需要关闭标记
?>
。事实上:。发生了什么?它正在调用我的java程序。但是我的java程序没有打印值phonecode,所以我认为它没有从php命令中获取值。。。。提前谢谢这里似乎