Steven
Steven

Reputation: 1256

Share JDBC connection with Runnable JAR

I have an app that will rely on a 3rd party JAR file to execute some code, this JAR has been developed by our client, we need to include a call to the JAR in our app and they have the hope that we can send the current SQL Connection as a parameter for the JAR

Having no previous experience working with this kind of scenario we all agreed it should be pretty straight-forward, but we overlooked the fact that the main class's main method only receives an array of Strings as parameter

I've googled my head off, but can't find a similar need, is it just completely off the books and impossible to do?

The call I was hoping to implement would look something like this:

final Process command = re.exec("java -jar ./MyClientsJar.jar " + arg1 + " " + arg2 + " " + SQLConnection);
      command.waitFor();

But of course, when we try to define a main method like this in the client's source code:

  private void MyClientsJARMainClass(String args[], Connection con){}

Eclipse's JAR export tool fails to find the main class

Any ideas other than sending user, url and pass arguments?

Upvotes: 0

Views: 104

Answers (1)

hkn
hkn

Reputation: 1448

You are creating a new process when you call re.exec. Since its a new process you cannot share your connection defined in another process.

Try to use the jar as a library. Import the class and make them a public method that accepts the connection as a parameter. For example:

import MyClientsJARMainClass
...
String[] args = { arg1, arg2 };
MyClientsJARMainClass.mainWithConnection(args, connection);

Your client adds a method like this in their class:

public static void mainWithConnection(String[] args, Connection connection)

Upvotes: 1

Related Questions