jax
jax

Reputation: 38633

Sending complex arguments to a java program

I need to supply a binary license file to a java program. What is the best way to do this? The data will change all the time so I don't want to have to write a file and then read it back every time.

The Data is coming from a PHP page

Upvotes: 0

Views: 277

Answers (4)

Powerlord
Powerlord

Reputation: 88806

I will be using the php exec() command which acts just like your were typing on the command line

In your Java class, you'll have a method named main that look like this:

public static void main(String[] args) {
    //If this is just a single binary string, args[0] will contain the license.
}

If you send the license as the first argument, args[0] will be your incoming license.

Now, having said that, you may want to base64 encode the license before sending it. This will make sure the license file is not affected by character sets as it is transmitted.

PHP has the function base64_encode() for this purpose.

Java strangely does not have Base64 decoding built-in (not officially anyway). Apache Commons Codec includes a decoder, though.

Again, assuming the license is the first thing passed in:

import org.apache.commons.codec.binary.Base64;

public class GiveMeANameHere {

    public static void main(String[] args) {
        Base64 decoder = new Base64();
        byte[] license = decoder.decode(args[0]);

        // Do whatever you need to with the license.
        // Other strings passed in will appear as elements in args array
    }
}

Not that the Java code will need to be passed the Apache Commons Codec jar as it's classpath, using either -cp path/to/commons-codec-1.4.jar or if you package your program as a jar, in its manifest file.

Upvotes: 2

sechastain
sechastain

Reputation: 613

Save the URL of the license file in user preferences (java.util.prefs). If the user preferences is new or the URL and/or its contents are no longer valid, then prompt the user for a new URL (and save it back into the preferences). You can then get the contents of the URL view java.net.URL.openStream() - works just as well for files or web addresses.

Upvotes: 1

OlimilOops
OlimilOops

Reputation: 6797

what about handing over the path to the file?
OK
You could use something like "interprocesscommunication"
- Remote Method Invocation (RMI)
http://java.sun.com/products/jdk/rmi/
- Simple Object Access Protocol (SOAP)
http://www.soaprpc.com/software/
http://java.sun.com/xml/jaxm/
http://ws.apache.org/soap/features.html

Upvotes: 0

justkt
justkt

Reputation: 14766

Sounds like you want to read an InputStream coming directly via HTTP. If you end up going a file upload route, Commons FileUpload has done a lot of your work for you.

Upvotes: 0

Related Questions