Solved

using -cp at runtime

Posted on 2002-05-01
25
345 Views
Last Modified: 2013-12-29
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.

Thanks.
0
Comment
Question by:pjmajer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
  • 5
  • +2
25 Comments
 
LVL 2

Accepted Solution

by:
CSuvendra earned 300 total points
ID: 6985318
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.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 6985321
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.
0
 

Author Comment

by:pjmajer
ID: 6985338
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.

Regards.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 2

Expert Comment

by:CSuvendra
ID: 6985380
After you do these steps, then call methods from Jar

****************************************************
import java.io.*;
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"));
     }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 6985424
> 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.
0
 
LVL 2

Expert Comment

by:mzimmer74
ID: 6985931
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
0
 
LVL 92

Expert Comment

by:objects
ID: 6987465
> java -cp . MyFile

That won't pick up any jar files.
0
 
LVL 92

Expert Comment

by:objects
ID: 6987470
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
0
 

Author Comment

by:pjmajer
ID: 6987664
CSuvendra:

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.

thx.
0
 

Author Comment

by:pjmajer
ID: 6987710
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 6987723
Have you compiled the java file to produce a class file?
You can't run .java files, only .class files.
0
 
LVL 92

Expert Comment

by:objects
ID: 6987725
javac xyz.java
0
 
LVL 2

Expert Comment

by:CSuvendra
ID: 6987893
Hi,

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.
0
 

Author Comment

by:pjmajer
ID: 6988750
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.

regards
0
 
LVL 2

Expert Comment

by:mzimmer74
ID: 6989375
Try this for a "MessageBox" equivalence:

String classpath = ...; //put in the code to get the classpath
JOptionPane.showMessageDialog(null, classpath, "cp", JOptionPane.INFORMATION_MESSAGE);
0
 

Author Comment

by:pjmajer
ID: 6991458
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.

Regards
0
 

Author Comment

by:pjmajer
ID: 6991490
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 <mailto:slbain@netobjectives.com> 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("some.class.that.is.in.%x%");
> > throws an exception.
0
 
LVL 92

Expert Comment

by:objects
ID: 6992463
As I mentioned above, just package your class as an executable jar file, and specify the required jars in your manifest :-)
0
 
LVL 2

Expert Comment

by:CSuvendra
ID: 6992979
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.
0
 

Author Comment

by:pjmajer
ID: 6993028
objects.

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.
0
 
LVL 92

Expert Comment

by:objects
ID: 6993040
> 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.
0
 

Author Comment

by:pjmajer
ID: 6994163
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 6995258
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);
0
 
LVL 2

Expert Comment

by:CSuvendra
ID: 6995635
http://java.sun.com/docs/books/tutorial/jar/api/index.html

Check out the source provided in

JarClassLoader.java
0
 

Author Comment

by:pjmajer
ID: 7259402
Original question was never answered but I want to close this question out anyways.  Thanks for your effort.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Maven Project: Hibernate Dependencies Conflict 10 94
Chrome and Firefox Java 5 88
jmss example java 2 48
Java class and jar 3 61
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question