1

I'm programming an application in java that stores data and images to a pdf. I'm using pdfbox. I saw that pdf box has some jar like the one for data encryption that has to be run by command lines.

Is there a way to run those jar in one of the functions of my class? I mean start the encryption processed by this jar after an event. How do i launch command lines from within my class?

For example:

if(IHaveData() && FileCreated())
*Encrypt the data*

Thanks in advance to everyone who wants to help even if the question may be stupid or something

Igr
  • 965
  • 4
  • 13
  • 26
  • read this for the command line thing http://stackoverflow.com/questions/8496494/running-command-line-in-java – fGo May 29 '13 at 10:06
  • check these links if it helps you: http://stackoverflow.com/questions/1794702/how-to-run-a-jar-file-from-inside-another-java-program http://stackoverflow.com/questions/4936266/execute-jar-file-from-a-java-program – Manish Kumar May 29 '13 at 10:07

1 Answers1

2

We do not run jars. We run applications. Java applications consist of a collection of classes and resources that can be stored either in file system or in jar files.

To run java application you should use command line like

java -cp one.jar;two.jar;three.jar Main

where

  • x.jar - the jar files
  • ; - separator for windows. Use : for unix
  • Main is a fully qualified class name of your entry point.

You obviously can run any command line from java program using Runtime.getRuntime().exec() or using ProcessBuilder. "Any" means that you obviously can run java application from other java application.

However this way has a lot of obvious disadvantages. Instead you should run API that is exposed by library you are using. If it does not expose convenient API you can in worse case directly call the main method from your program:

Main.main(new String[] {param1, param2})

AlexR
  • 114,158
  • 16
  • 130
  • 208