Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to run jar files with classpath?

Posted on 2001-06-20
11
Medium Priority
?
2,966 Views
Last Modified: 2013-11-23
Hi

I'm trying to run my own jar file (and I can) but it is complaining that it can't find a class in another jar file. I've been trying the following:

java -cp path_to_otherfile\otherfile.jar -jar myfile.jar

but it won't work. I am certain that the classpath is correct and everything works fine if I run the extracted class file (from the jar) directly:

java -cp path_to_otherfile\otherfile.jar myclass

Any ideas?
0
Comment
Question by:boomerang061797
[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
11 Comments
 

Expert Comment

by:rameshnut
ID: 6210910
hi boomerang


just try this

java -cp .;path_to_otherfile\otherfile.jar -jar myfile.jar

(i have added a dot and a semi colon,just try it)


rameshnut

0
 
LVL 1

Expert Comment

by:mrachow
ID: 6210944
Try
java -cp "pathToOthers\other.jar;pathToYour\YourJar.jar" packageOfYourClass.YourClass

Greetings
Michael
0
 
LVL 92

Accepted Solution

by:
objects earned 150 total points
ID: 6212508
Add the following to your MANIFEST for myfile.jar:

Class-Path: other.jar

I think this assumes the two jars are in the same directory. You may also specify a relative location for the jar.

0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

Author Comment

by:boomerang061797
ID: 6213566
To rameshnut: I tried that first and it still didn't work. To objects: I tries this and it still can't find the file, but this seems like the right way to go. However, I am trying to refer to another jar file on my harddisk and it has nothing to do with my jar file. It will therefore never be part of my actual jar file. How can I refer to a jar file that is not part of either the system class path or included in my jar file?
0
 
LVL 92

Expert Comment

by:objects
ID: 6213584
If you don't know the location of the other jar relative to your jar at the time of creating your jar then I think you'll either have to:

- ensure that the other jar is placed in the 'ext' directory, or

- run the class without using the -jar option.

0
 
LVL 92

Expert Comment

by:objects
ID: 6213598
From the (-jar) doco:

"The JAR file is the source of all user classes, and other user class path settings are ignored. "


0
 

Author Comment

by:boomerang061797
ID: 6213692
What do you mean by the ext directory? I'm running on NT. I've tried running the jars in the same directory, but that didn't make a difference.
0
 
LVL 92

Expert Comment

by:objects
ID: 6213717
> What do you mean by the ext directory?

There is an ext directory inside the lib directory of your JAVA_HOME directory. Any jars in that directory will be included in your classpath automagically.


http://java.sun.com/j2se/1.3/docs/guide/extensions/extensions.html

> I've tried running the jars in the same directory

If the jars are in the same directory then providing 'Class-Path' in your manifest should do the trick.
0
 

Author Comment

by:boomerang061797
ID: 6234357
Just one last question; is it common to place JDBC driver jar files in the extension directory of the JRE on the server or is there a better solution?
0
 
LVL 92

Expert Comment

by:objects
ID: 6234595
Can't see a problem with doing that.
The only reason I can think of for not putting any jar in the ext folder is if you want to control the load order, for example if you have two versions of the same class.
O/wise I'd say dump them all in there :)
0
 

Author Comment

by:boomerang061797
ID: 6235405
I'm still working on trying to get the ext directory to work properly on my win2000 machine. No trouble on linux though.
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

688 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