简体   繁体   中英

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

I am attempting to run the following Java script from the command line via PHP on a Windows/Xampp environment.

//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() {
}

I was previously running the script using Processing 2.2.1 and have compiled the Java down to an .exe that I am hitting with PHP's system() command. I need to be able to pass at least two variables along to the script above and set them as the UUID and NAME fields in the myClient.write() function.

It's been an age since I have written any Java and any attempt to wrap the script above in a class causes errors. Can anybody advise on how I would pass arguments into the script and collect them on the other side?

Many thanks!

Every Processing sketch (PApplet) has an args property that gives you access to the list of command line arguments. According to the docs:

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

So something like this should work:

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() {
}

Calling a Processing app from PHP to call another PHP script sounds a bit convoluted. What are you trying to achieve exactly ? (Perhaps there's an easier way)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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