Category Archives: Dr. Tinelli

Invoke a binary inside of .jar

I’m working on a Java GUI for an OCaml executable. I need to execute a binary that resides in a .jar. It turns out that binaries cannot be executed while in the .jar. The binaries have to be copied from the .jar and placed on the host machine. I chose to place the binaries in a temporary file. Then the .jar can use system commands to interact with binaries it needs to execute.

The following thread led me in this direction. Thank you, stackoverflow.com!

http://stackoverflow.com/questions/4764347/extract-and-load-dll-from-jar

Here is some example code to copy a file from your .jar to the host machine.

   

try {
    // Get a handle (as a stream) on the binary you want to move from the .jar to a folder on the system
    InputStream in = Toolkit.getDefaultToolkit().getClass().getResourceAsStream("/path/to/binary/in/jar");

    // Get a handle on where you want the binary to be saved.
    File fileOut = new File("/path/you/want/to/place/your/lib/binaryname");

    // make sure the path to the file exists
    fileOut.mkdirs();

    // Create an output stream to put data into.
    OutputStream out = new FileOutputStream(fileOut);

    // Copy the bytes from our input stream to our output stream.
    byte[] buf = new byte[1024];

    int len;

    while((len = in.read(buf)) > 0) {

        out.write(buf, 0, len);

    }

    in.close();
    out.close();

} catch (Exception e) {
    //handle
}

After a successful copy you can then use the Runtime to execute your command.

  

try {
    String command = "path/to/binary -args";

    Process p = Runtime.getRuntime().exec(command);
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

    String s = null;

    while ((s = stdInput.readLine()) != null) {
        System.out.println(s);

    }

    // read any errors from the attempted command
    System.out.println("Here is the standard error of the command (if any):n");

    while ((s = stdError.readLine()) != null) {
        System.err.println(s);
    }

}

catch(Exception e){
    //handle
}

KIND Candy Coat

I’m still working on KIND, however, I am now developing a Java based GUI that acts as a wrapper for the existing OCaml based tool. This GUI will be a persistent wrapper that allows the user to avoid interacting with the command line interface, as KIND executes. As I progress through development I’ll post screenshots of the GUI and compare it to its command line counterpart.

At the moment I’m researching the best way to integrate a Java GUI and an OCaml project. I’m debating between using a tool that converts OCaml source to Java source or serializing data and transmitting it between two processes.

This semester my job amounts to making all interactions with KIND easier and more convenient. Hence all of the GUI development!

Where are those installers?

Last week I spent a couple of days looking for an open source cross-platform installer that can dynamically download data (dependencies) from the web. I couldn’t find anything that suited my needs. 🙁 Subsequently, I’m creating an installer from scratch for KIND, an automatic verification tool for Lustre programs.

It’s a fun project, but I’m stuck using Java. I’m not a huge fan of Java when a GUI is required. I’m creating a GUI based installer. Thankfully, the NetBeans IDE supports drag and drop GUI development but looking at the auto-generated code-behind, which is mixed with developer code, looks horrible – it’s esoteric and verbose!