通过命令行将参数传递和处理到已编译的 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 的系统运行的 .exe( ) 命令。我需要能够将至少两个变量传递给上面的脚本,并将它们设置为 myClient.write() 函数中的 UUID 和 NAME 字段。

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

非常感谢!

每个 Processing sketch (PApplet) 都有一个 args 属性 使您可以访问命令行参数列表。 根据文档:

Command line options passed in from main(). This does not include the arguments passed in to PApplet itself.

所以像这样的东西应该可以工作:

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 脚本听起来有点费解。你到底想达到什么目的? (也许有更简单的方法)