using -cp at runtime

I have a .java file thats accessing a .jar file.  When I install my software that installs the .java file that calls the .jar file, I will get 'Java Exception Occurred' unless I explicitly go to the CLASSPATH system env variable, and put in the path to the .jar file.

I do not want to do this.  I want to be able to install the software and then just run the code with no errors and with the .jar file being found.

I also want this to be able to run on any OS.  So this is the problem I need a solution for:

In Java:
get the current location of the where the .java file is located (since that is the same location that the .jar file will be) and use -cp somehow to specify in the .java file where to find the .jar file without giving an exception.  Also, the path of where these files will be will be dynamic.  So I'll have to be able to get the path at runtime.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I guess you need to set the classpath using System.setProperty() - if security manager permits.

1. get Property java.class.path (a)
2. get Property file.separator (b)
3. get Property path.separator (c)
4. get Property user.dir (d)
5. Create a string as (a) + (c) + (d) + (b) + (JAR file name)
6. set Property java.class.path

This should be done before you invoke the methods in Jar File. Check if this works.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Have you tried placing the jar in the jdk/jre's (whatever version) lib/ext directory?  It may work, especially if the jar is a collection of library functions or so on.
pjmajerAuthor Commented:
I cannot put the java file in the jdk/jre lib/ext directory.  It needs to be packaged with my other files.

I need a clarification on the steps you've outlined.  They seem as if they may work.  But I'm not sure how to do all of if any of the steps in java.  If you could elaborate on them all that would be great.

Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

After you do these steps, then call methods from Jar

import java.lang.*;

public class NoName {
     public static void main(String [] args) {
          String a = System.getProperty("java.class.path");
          String b = System.getProperty("file.separator");
          String c = System.getProperty("path.separator");
          String d = System.getProperty("user.dir");

          String e = a + c + d + b + "myJar.jar";
          System.setProperty("java.class.path", e);

          System.out.println("ClassPATH: " + System.getProperty("java.class.path"));
Mick BarryJava DeveloperCommented:
> since that is the same location that the .jar file will be

If you jar is in the same dircectory as your class file then simply load the jar when your class loads up using URLClassLoader.

URLClassLoader loader = new URLClassLoader(urls);
Class c = loader.loadClass(classname);

If you're jar files are elsewhere then you'll need to search the disk for them.
You may have tried this already, but if you jar file is in the same directory that your java file is in (and the same directory that you're running your java file from) you shouldn't need anything more than "." as your classpath.  Basically, if your java file is called MyFile and your jar file is called MyJar.jar you should be able to do:

   java -cp . MyFile

and if that doesn't work try:
   java -cp .;./MyJar.jar MyFile
Mick BarryJava DeveloperCommented:
> java -cp . MyFile

That won't pick up any jar files.
Mick BarryJava DeveloperCommented:
Another option would be to distribute your class file as an executable jar. This way you could include reference to the required jars in the manifest.
The jar could then simply be started using:

java -jar mynewjar.jar
pjmajerAuthor Commented:

The steps you provided worked when I ran a test class from the command line.  But when I launched my application which calls the .java file (which calls the 3rd party software) I got a java exception occurred message box.

Is there a way in java to print out the contents of java.class.path?  I can't use system.out.println because I'm not running from a command line - I'm running it at runtime.

pjmajerAuthor Commented:
One more thing - I think its giving me the exception not because its not finding the .jar file, but because its not finding the .class file.  I am running an Actuate Report that calls the .java file which in turn calls the .jar file.  I believe the Actuate report call to the .java file is failing because it can't find the .class.  Therefore the code in the .java file, I believe, is useless unless I can set the environment variable in the VB.  I know I can use ENVIRON$("CLASSPATH") to get the current value of the environment variable, but I don't know how I can append to it in VB and then set it.
Mick BarryJava DeveloperCommented:
Have you compiled the java file to produce a class file?
You can't run .java files, only .class files.
Mick BarryJava DeveloperCommented:

You must be using some debugger to log messages to a file. You can dump the class path into the debugger file. Or if it is a AWT based project, display in a MessageBox (Option Pane - JOptionPane)

BTW, I guess the problem lies in finding the dependencies for the JAR file you mentioned. Please confirm if there is a single Jar file or are there multiple Jar files dependency. If that is the case, you need to include the dependencies in the CLASSPATH too.

And you can always test if the Class file giving the error is in the JAR by unzipping it ;-)

Also, the dependency may not be JAR/ Class at all. It may be a library whose path in Window$ need to be set in PATH env variable, and in Unix LD_LIBRARY_PATH variable. Find out what exactly is missing.

I need to check how to set ENVIRON in VB but that can be done when I get some time out of my job :-(. A better option is asking this question in VB forum, where you can get a very prompt reply.
pjmajerAuthor Commented:
I did ask the VB question in the VB forum.  Nobody is helping out too much with that.

And yes, I did mean the .class file not the .java file.  I did compile with javac (i'm not that bad at java :-) )

There is only 1 .jar file that I use.  I don't think its the PATH env variable because I have put the location of the .jar file in my CLASSPATH and it works.

Is there any java code that will pop up a message box with the CLASSPATH in it after I have edited it?  I tried "MessageBox" and my file did not compile.  I still need help with this - I'm really lost and pressed for time.

Try this for a "MessageBox" equivalence:

String classpath = ...; //put in the code to get the classpath
JOptionPane.showMessageDialog(null, classpath, "cp", JOptionPane.INFORMATION_MESSAGE);
pjmajerAuthor Commented:
OK, I tried using the JOptionPane and it popped up with the classpath.  It also had my string appended to the end of it so it looked like it was adding the correct location to the .jar file.  However, I was still getting the 'Java Exception Has Occurred' - would any of you know why?  Am I adding the location of the .jar file too late?  I do have at the top of my java file 'import InkTools.*;' - the name of the .jar file is InkToolsLib.jar.

Another problem is that my java class is called many times during one session - so it kept appending the same path to the CLASSPATH over and over.  I have to have one simple check before I set the CLASSPATH to see if user.dir is currently in the CLASSPATH - and if so, do not append.  Is there an easy way to do this with SubStr or something?

Thanks for your help so far.  I would love to get these issues resolved ASAP so I can award you points.

pjmajerAuthor Commented:
One more thing - I actually got this information from another source and it seems to say that you cannot use System.getProperty and System.setProperty at runtime - it does not affect the existing Virtual Machine - maybe this is why I see the correct path, but get the exception messages - any comments?

Not a bug, exactly

Posted by Scott Bain <> on February 12, 2002 at 5:51 PM
This is not really a bug. The java System property java.class.path is used to build a linked link when the JRE is instantiated, then is not re-read.

Therefore, changes you make to the property don't really do anything to the existing virtual machine.

-Scott Bain-

>> There is a bug in Java. Because i got the same problem while i was working on my project.
> This bug can be seen by going to sun java web site.

> > How can I change classpath at runtime?

> > System.setProperty("java.class.path","%x%"); is executed,
> > System.getProperty("java.class.path"); shows new classpath, but
> > Class.forName("");
> > throws an exception.
Mick BarryJava DeveloperCommented:
As I mentioned above, just package your class as an executable jar file, and specify the required jars in your manifest :-)
This is interesting. I was unaware of it. I would then go by objects post. Pack the classes into an executable jar and define in manifest.
pjmajerAuthor Commented:

please explain on what you mean.  I'm really am not experienced with what you are saying.  I don't know how it would work for me either - it seems as if you said to start the jar as:

java -jar mynewjar.jar

But I will not call my class/jar file from the command line - therefore cannot use the 'java -jar mynewjar.jar'.  I'm creating a java object in VB and the VB calls the class which in turn calls this elusive jar file all at runtime.  No command line calls are made.

If you think your suggestion would work in my case, please explain what you mean and how I could achieve it.
Mick BarryJava DeveloperCommented:
> I'm creating a java object in VB and the VB calls the class

Then you should be setting the classpath correctly in VB before starting the VM.

Otherwise try my other suggestion above and use URLClassLoader to load your class.
pjmajerAuthor Commented:
I found out you cannot set the CLASSPATH in VB.  Not dynamically like I want.  I tried going down that rout.

You mentioned 'executable jar and define in manifest' - this would not work for me would it since I'm not using the command line.

Also, I looked into the URLClassLoader and it did not see how that would benefit me or how I could use that - please explain.
Mick BarryJava DeveloperCommented:
Are you using JNI to call Java from VB?

> Also, I looked into the URLClassLoader and it did not
> see how that would benefit me or how I could use
> that - please explain.

URLClassLoader loader = new URLClassLoader(urls);
Class c = loader.loadClass(classname);

Check out the source provided in
pjmajerAuthor Commented:
Original question was never answered but I want to close this question out anyways.  Thanks for your effort.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.