1

I need to reference some jar files when executing a Java program. The problem is that those jar files will not be in a standard location ever. That means when the Java program starts up I need to find out where those jar files are and update any references in the Java program. I have no idea how to do this though. Any help would be appreciated.

Scenario:
I have a Process A (written in .NET) that will start Process B (a Jar file). Process B needs to reference tons of Jar files(250MB). The problem is that the location of the jar files will change.

Several Examples:

C:\Program Files (x86)\TestProgram 
D:\OtherProgram 
E:\Program Files\ThirdLocation

So, either Process A or B will read the registry (on a Windows Server) to find out where the Jar files are located. Then Process B needs to "magically point all the references in the code to those jar files".

Problem:
The problem is that I don't know how to "magically point all the references in the code to those jar files". I assume I have to dynamically load the jars somehow, or update the references when I start Process B.

I have tried to configure the build path, but I don't think I did it correctly, so it didn't work as expected.

Any ideas on how to do this? Thanks for any help!!

Greg
  • 3,086
  • 3
  • 26
  • 39
  • Have you looked into using Maven for managing dependencies? – jbranchaud Dec 28 '11 at 00:51
  • I posted a utility method that may help here. [Finding he path to a jar from a class inside it?](http://stackoverflow.com/questions/8508114/finding-the-path-to-a-jar-from-a-class-inside-it/8508874#8508874). – OldCurmudgeon Dec 28 '11 at 01:35
  • I posted a utility method that may help here. [Finding he path to a jar from a class inside it](http://stackoverflow.com/questions/8508114/finding-the-path-to-a-jar-from-a-class-inside-it/8508874#8508874). – OldCurmudgeon Dec 28 '11 at 01:36
  • The jar files I am referencing is a horrible SDK(Business Objects(SAP)), and I just needed to reference their SDK Jar files. Because of that, from the very little I know about Maven, I don't think it would help, and the other code, from Paul, I don't think will help. Thanks for the suggestions though, they were helpful in pushing me to look at other options. (My final solution in Ravi's post) – Greg Dec 29 '11 at 16:26

1 Answers1

0

One of the arguments of the Java runtime (java.exe or javaw.exe) is -cp. It is used to set the classpath as described here.

The nice thing about Java 6 is that you can use wildcards while setting your classpath. So supposing that you need to load all JARS inside one folder, you can do something like this:

-classpath "../lib/*" 

For the command line, -classpath and -cp are the same thing.

To separate the classpaths, you must use a semicolon. You can easily configure your whole classpath using the wildcards above or, if that doesn't work, then you will need to specify each JAR file manually. Be aware of the command prompt max length in Windows while doing that.

Ravi Wallau
  • 10,416
  • 2
  • 25
  • 34
  • What I'm going to end up doing is package the jar files I need with the app, instead of trying to find them on the local machine. Knowing about the class path helped a bit with the problem, but I couldn't get the "*" to pick up all of the jar's properly. – Greg Dec 29 '11 at 17:05
  • That's a better solution indeed. You can use something like Maven to do the job for you. – Ravi Wallau Dec 29 '11 at 17:58