0

I have a command line application which I need to reference from another jar file and what I don't want to do is copy and paste the existing code into the project that contains the source files for the other application, since this approach will result in maintainability issues.

I have thought about doing the following:

  1. deploying 2 jar files; running the command line app as a Process (the problem with this approach is that I don't want to deal with orphan processes).

  2. deploying 2 jar files; calling the main method of the command line app using reflection. (I'm not sure this is a good idea).

  3. Somehow referencing the project that contains the source code for the command line application, and write an ant file that copies the class files into the jar file for the other application.

Which of this approach makes most sense? if the answer is #3, how exactly do I need to set up the eclipse project and/or write the ant file such that the class files I need are included in the new jar file?

John Smith
  • 147
  • 1
  • 5
  • Could you explain in more detail the problem with option 1? That's what I would do... – gsgx Feb 18 '13 at 06:02
  • The problem with #1 is that if the user closes the app while the child process is running, the child process will continue running in the background. At least based on the research I have done, there is no simple way to force the child process to exit the moment the application that started it exits. Also, Runtime.getRuntime().addShutdownHook(Thread) does not work. It would only work if I were to call System.exit(int), not if I were to forcibly close the app. – John Smith Feb 18 '13 at 06:07
  • I see. I still think 1 is the best solution, and the command line jar should be able to check to see if it still has a parent process so you can possibly avoid the orphan situation: http://stackoverflow.com/questions/269494/how-can-i-cause-a-child-process-to-exit-when-the-parent-does – gsgx Feb 18 '13 at 06:16
  • Why not just put the jar for project 1 in the classpath from project 2? Or is there something you need that project 2 cannot do? – Thorbjørn Ravn Andersen Feb 18 '13 at 07:34

1 Answers1

0

In my opinion, I am more inclined to Approach 1.

Assume your command line application is Jar1 and the referenced 3rd party jar is JAR2. Just add JAR2 to classpath. That's it. You can invoke all the desired classes from Jar1. Since it's a command line application and you don't want to deal with orphan process, the ideal way to handle with it is, you should handle it from your command line application to kill orphan process, similar to what you do from your finally block of try-catch.

I am disoriented towards your Approach 2 as reflection is very discouraged/outdated as it gets very difficult to debug in case of any issues

Vinod Jayachandran
  • 3,726
  • 8
  • 51
  • 88