0

I want to create a GUI app in java for signing j2me app which is done by JadTool.jar but it is a Command Line Interface Apps. So I just want to use it as library and pass the parameters in program. How it can be done?

ssa
  • 199
  • 5
  • 13

2 Answers2

2

Check out Runtime. It will allow you to execute a command. You can use this to start your command line interface library.

Edit: Ah, I didn't read care carefully earlier. If you're using a Java library starting a separate process is not the best solution.

Just reference the JadTool jar from your project. If the functionality you need isn't accessible in the library, edit the source and recompile. Make sure JadTool's license allows this.

If you're against editing the source (or not allowed) try using reflection to invoke the private run method you need.

William Morrison
  • 10,953
  • 2
  • 31
  • 48
1

A jar is just a library of classes, the fact that it can be run from the command line is caused by the presence of a main method in a class. As jadtool's source is available it's easy to see its very simple main:

public static void main(String[] args) {
    int exitStatus = -1;

    try {
        new JadTool().run(args);
        exitStatus = 0;
    } catch (Exception e) {
        System.err.println("\n" + e.getMessage() + "\n");
    }

    System.exit(exitStatus);
}

Unfortunately, that run() method is private, so calling it directly from another class won't work, leading to a reduced set of options:

  1. @WilliamMorrison 's solution of going via Runtime - not really a library call, but it would work.
  2. see Any way to Invoke a private method?
Community
  • 1
  • 1
fvu
  • 32,488
  • 6
  • 61
  • 79
  • Currently I only need `performAddJarSigCommand(String[] args)`, so if convert it to `performAddJarSigCommand(String par1, string par2...)` and remove `run()` edit `main` method to remove dependency from command Line and rebuild the source then can I use it as library? – ssa Jan 19 '14 at 19:42
  • 2
    @Dark_Prince Yes, you can. I've updated my answer to cover that. – William Morrison Jan 19 '14 at 19:47
  • @Dark_Prince `performAddJarSigCommand` is also a private method, so the same suggestion still applies. – fvu Jan 19 '14 at 22:32
  • I have edited source and changed private to public with other modifications and it is working perfectly. Thanks to you and @WilliamMorrison . – ssa Jan 20 '14 at 17:58