通过命令行传递和处理参数到编译后的Java exe


Passing and handling arguments via command line to a compiled Java exe

我试图在Windows/Xampp环境下通过PHP从命令行运行以下Java脚本。

//Unlock    
import processing.net.*; 
Client myClient; 

void setup() { 
  size(300, 300)
  // Connect to the local machine at port 10002.
  // This example will not run if you haven't
  // previously started a server on this port.
  myClient = new Client(this, "127.0.0.1", 6789); 
  // Say hello
  myClient.write("UUID=F326597E&NAME=Name");
  exit();
} 
void draw() {
}

我以前使用Processing 2.2.1运行脚本,并将Java编译成我使用PHP的system()命令访问的.exe。我需要能够将至少两个变量传递给上面的脚本,并将它们设置为myClient.write()函数中的UUID和NAME字段。

我已经很久没有写过Java了,任何将上面的脚本包装在类中的尝试都会导致错误。有人能告诉我如何将参数传递到脚本中并在另一边收集它们吗?

多谢!

每个处理草图(PApplet)都有一个args属性,可以让您访问命令行参数列表。根据文档:

从main()传入的命令行选项。这并不包括参数传递给PApplet本身。

这样就可以了:

import processing.net.*; 
Client myClient; 
String uuid = "F326597E";
String name = "Name";
void setup() { 
  size(300, 300);
  if(args.length < 2) System.err.println("uuid,name args missing, using defaults: " + uuid+","+name+"'n");
  else{
    uuid = args[0];
    name = args[1];
    println("parsed args uuid: " + uuid+"'tname:" + name);
  }
  // Connect to the local machine at port 10002.
  // This example will not run if you haven't
  // previously started a server on this port.
  myClient = new Client(this, "127.0.0.1", 6789); 
  // Say hello
  myClient.write("UUID="+uuid+"&NAME="+name);
  exit();
} 
void draw() {
}

从PHP调用Processing应用程序来调用另一个PHP脚本听起来有点复杂。你到底想达到什么目标?(也许有更简单的方法)