Radan Radan -4 years ago 131
Java Question

java class path issue with properties file

I have a batch file to set class path before calling the java main method, see code below

SET CLASSPATH=%CLASSPATH%;libs/xyz1.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz2.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz3.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz4.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz5.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz6.jar
SET CLASSPATH=%CLASSPATH%;/resource

java -classpath %CLASSPATH% com.xyz.main


if I keep my properties file inside the resource folder it works fine, but if I leave it in root folder it doesn't work.

SET CLASSPATH=%CLASSPATH%;libs/xyz1.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz2.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz3.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz4.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz5.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz6.jar

java -classpath %CLASSPATH% com.xyz.main


the above class path setting don't work am getting null pointer exception

SET CLASSPATH=%CLASSPATH%;libs/xyz1.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz2.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz3.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz4.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz5.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz6.jar
SET CLASSPATH=%CLASSPATH%;/

java -classpath %CLASSPATH% com.xyz.main


the above class path setting don't work am getting the same null pointer exception.

Am using java util properties as below.

InputStream inputStream = this.getClass().getResourceAsStream("/abc.properties");
getProperties().load(inputStream);


it will be really nice if someone can help me find, why this happens. thank you in advance.

Answer Source

Include complete class path

java -classpath .;%CLASSPATH% com.xyz.main

you can also set multiple specification Refer the below link, http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/classpath.html

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download