Dmitry_Bond
asked on
Why ClassPath parameter does not work when I run a JAR?!
Hi.
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:
ERROR(java.lang.ClassNotFo undExcepti on): COM.ibm.db2.jdbc.app.DB2Dr iver
So, I think that means that java.exe ignored my -cp "%JdbcRef%" parameter because db2java.zip 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("COM.ibm.db2 .jdbc.app. DB2Driver" ) if code in my JAR starts another copy of java.exe and run the same JAR with different parameters?
Notes:
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.
Regards,
Dmitry.
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:
ERROR(java.lang.ClassNotFo
So, I think that means that java.exe ignored my -cp "%JdbcRef%" parameter because db2java.zip 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("COM.ibm.db2
Notes:
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.
Regards,
Dmitry.
@echo off
set JavaDir=C:\Program Files (x86)\IBM\SQLLIB\java
set JavaHome=%JavaDir%\jdk\jre
set JdbcRef=%JavaDir%\db2java.zip
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
private string dbDriverName = "COM.ibm.db2.jdbc.app.DB2Driver";
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));
try
{
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()));
exc.printStackTrace();
}
MiscUtils.toLogger(Level.WARNING, String.format("Trying to connect db (%1$s) as (%2$s) ...", this.dbName, this.dbUser));
try
{
//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()));
exc.printStackTrace();
}
MiscUtils.toLogger(Level.WARNING, String.format("Trying to configure db connection..."));
try
{
this.dbConn.setAutoCommit(false);
}
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you can use this:
"%JavaHome%\bin\java.exe" -cp "JavaCslMon.jar";%CLASSPAT H% TxMonitor -mode=test -test=database
"%JavaHome%\bin\java.exe" -cp "JavaCslMon.jar";%CLASSPAT
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 ;%CLASSPAT H%
"%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.
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
"%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.
:-)
ASKER
"%JavaHome%\bin\java.exe" -cp "JavaCslMon.jar;%JdbcRef%"
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.ClassNotFoundEx
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\QT
And there is I can see the required ZIP file - C:\PROGRA~2\IBM\SQLLIB\jav
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?