Why ClassPath parameter does not work when I run a JAR?!

Posted on 2010-08-28
Last Modified: 2012-06-27

I have created a JAR file in Eclipse (Eclipse Java EE IDE for Web Developers. Version: Helios Release. Build id: 20100617-1415).

Then I'm starting it using a bat file (see enclosed code # 1) but I'm getting the following error:

So, I think that means that java.exe ignored my -cp "%JdbcRef%" parameter because is exactly the file which contains the required class.

Question are:
1) Why java ignoring ClassPath I have specified?
How to make it working?

2) Is it possible to override (or add more pathes to) the CLASSPATH specified inside a JAR in the ".classpath" file?

3) Why it hang at the Class.forName("") if code in my JAR starts another copy of java.exe and run the same JAR with different parameters?

a) I know for sure - database parameters are correct - when I'm running it under debugger in Eclipse it works fine. So, problems exists only when I run a JAR.

b) Eclipse configured to use the same JRE (see the %JavaHome% in a bat file example). So, it cannot be because of different JRE.

@echo off

set JavaDir=C:\Program Files (x86)\IBM\SQLLIB\java
set JavaHome=%JavaDir%\jdk\jre
set JdbcRef=%JavaDir%\

echo "%JavaHome%\bin\java.exe" -cp "%JdbcRef%" -jar JavaCslMon.jar TxMonitor -mode=test -test=database

"%JavaHome%\bin\java.exe" -cp "%JdbcRef%" -jar JavaCslMon.jar TxMonitor -mode=test -test=database

Open in new window

private string dbDriverName = "";
private string dbName = "PT2010A";
private string dbUser = "uzver";
private string dbPassword = "pazzw0rd";

public Connection getConnection() throws Exception
	if (this.dbConn != null) return this.dbConn;
	MiscUtils.toLogger(Level.WARNING, String.format("Trying db driver (%1$s) ...", this.dbDriverName));
		Class c = Class.forName(this.dbDriverName, false, this.getClass().getClassLoader());
		this.dbDriver = (Driver)c.newInstance();
		MiscUtils.toLogger(Level.WARNING, String.format("* Db driver (%1$s): found", this.dbDriverName));
	catch (ClassNotFoundException exc)
		MiscUtils.toLogger(Level.WARNING, String.format("! Db driver (%1$s).ERROR(%2$s): %3$s", this.dbDriverName,
				exc.getClass().getName(), exc.getMessage()));
	MiscUtils.toLogger(Level.WARNING, String.format("Trying to connect db (%1$s) as (%2$s) ...", this.dbName, this.dbUser));
		//this.dbConn = DriverManager.getConnection(this.dbName, this.dbUser, this.dbPassword);
		Properties props = new Properties();
		props.setProperty("user", this.dbUser);
		props.setProperty("password", this.dbPassword);
		this.dbConn = this.dbDriver.connect(this.dbName, props);
		MiscUtils.toLogger(Level.WARNING, String.format("* Db successfully connected."));
	catch (SQLException exc)
		MiscUtils.toLogger(Level.WARNING, String.format("! Fail to connect database (%1$s)! ERROR(%2$s): %3$s", this.dbName,
				exc.getClass().getName(), exc.getMessage()));
	MiscUtils.toLogger(Level.WARNING, String.format("Trying to configure db connection..."));
	catch (SQLException exc)
		MiscUtils.toLogger(Level.WARNING, String.format("! Fail to configure database connection! ERROR(%1$s): %2$s", 
				exc.getClass().getName(), exc.getMessage()));
	return this.dbConn;

Open in new window

Question by:Dmitry_Bond
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
  • 2
  • 2
  • 2
LVL 86

Accepted Solution

CEHJ earned 250 total points
ID: 33550576
When using the -jar argument. the classpath is ignored. The Class-Path attribute inside the jar is used instead. You need to do
"%JavaHome%\bin\java.exe" -cp "%JdbcRef%";JavaCslMon.jar TxMonitor -mode=test -test=database

Open in new window


Author Comment

ID: 33550645
Waw, super! Yes, true, this works:

"%JavaHome%\bin\java.exe" -cp "JavaCslMon.jar;%JdbcRef%" TxMonitor -mode=test -test=database

BUT(!) next part of problem - why java.exe ignoring the CLASSPATH?
For example if I change command to

"%JavaHome%\bin\java.exe" -cp "JavaCslMon.jar" TxMonitor -mode=test -test=database

Then it stops to work with the "java.lang.ClassNotFoundException:" error. BUT WHY?!
I clearly see that the CLASSPATH environment variable contains the required path. So, if I add the "echo CP: [%CLASSPATH%]" command to a bat file I see following output:

CP: [.;C:\Program Files (x86)\Java\jre6\lib\ext\;C:\PROGRA~2\IBM\SQLLIB\java\;C:\PROGRA~2\IBM\SQLLIB\java\db2jcc.jar;C:\PROGRA~2\IBM\SQLLIB\java\;C:\PROGRA~2\IBM\SQLLIB\java\db2jcc_license_cu.jar;C:\PROGRA~2\IBM\SQLLIB\bin;C:\PROGRA~2\IBM\SQLLIB\java\common.jar]

And there is I can see the required ZIP file - C:\PROGRA~2\IBM\SQLLIB\java\
Question is - why java.exe cannot find it?
Is there any option to make java.exe use %CLASSPATH% + the pathes in "-cp" command line parameter?
LVL 92

Expert Comment

ID: 33550712
you can use this:

"%JavaHome%\bin\java.exe" -cp "JavaCslMon.jar";%CLASSPATH% TxMonitor -mode=test -test=database
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 92

Assisted Solution

objects earned 250 total points
ID: 33550722

Author Comment

ID: 33550755
Ok, thanks.
Seems I have solved my problems with CLASSPATH.
So, now my solution is:

use the following code in a bat file:

    set CLASSPATH=.\JavaCslMon.jar;%CLASSPATH%
    "%JavaHome%\bin\java.exe" TxMonitor

So, later when it starts another java.exe from application code it will re-use environment of existing process and should works fine.
LVL 86

Expert Comment

ID: 33552345

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
Java string replace 11 76
What's wrong with this code? 4 45
jmss example java 2 48
using executorService 3 19
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

734 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