2
cd ../../jobs
set CLASSPATH=.;../xyz.jar;../mysql-connector-java-5.1.6-bin.jar
java folser.folder1 ../Files/MySQL.xml
cd ..

I need to run the batch file from any directory. I have set the paths for java. Can anybody help me?

1
  • Is this Windows or Linux? Your use of "/" in the cd command indicates Linux but you talk about batch files rather than shell scripts.
    – paxdiablo
    Mar 3, 2009 at 5:11

4 Answers 4

3

Under *nix (e.g. Linux):

cd "`dirname \"$0\"`"
# your current directoy is now the script's directory
cd ../../jobs
set CLASSPATH=.:../xyz.jar:../mysql-connector-java-5.1.6-bin.jar
java folder.folder1 ../Files/MySQL.xml
cd ..
# when the script terminates, you are automatically
#  back to the original directory

Under Windows NT/XP/etc.:

SETLOCAL
PUSHD .
REM current directory has been saved and environment is protected
CD /d %~dp0
REM your current directoy is now the script's directory
CD ..\..\jobs
SET CLASSPATH=.;..\xyz.jar;..\mysql-connector-java-5.1.6-bin.jar
java folder.folder1 ..\Files\MySQL.xml
CD ..
REM before the script terminates, you must explicitly
REM return back to the original directory
POPD
ENDLOCAL
2
  • I don't think you're back in the original directory at the end for the Windows version. You should use setlocal/endlocal to ensure environment changes don't leak out of the script - you may also need to store the current path and drive and restore when finished.
    – paxdiablo
    Mar 3, 2009 at 5:16
  • And +1. Well, stuff me! I didn't even know about pushd/popd, I've been saving my PWD in an environment variable. Must remember to change that code next time it's checked out.
    – paxdiablo
    Mar 3, 2009 at 6:09
1

Although I can't comment on Vlad's answer (comments require more points than answers?!) I would always be wary of relying on:

CD /d %~dp0

because Windows can't CD to UNC paths and has a nasty habit of dropping you into %windir% instead with potentially catastrophic results.

Instead, although it is more long-winded, you are usually better off referring to %~dp0 (or a variable containing that value) each time you need a full path.

BAD:

cd /d %~dp0
rd temp

GOOD:

rd %~dp0\temp
0

You message was a bit garbled, I'm assuming you're saying that java is on the path but you can't properly run your application from a batch file. It looks like you are missing the classpath option (-cp) for java. Try this:

cd ../../jobs
set CLASSPATH=.;../xyz.jar;../mysql-connector-java-5.1.6-bin.jar
java -cp %CLASSPATH% folser.folder1 ../Files/MySQL.xml
cd ..
2
  • This does not answer the poster's question
    – vladr
    Mar 3, 2009 at 5:02
  • Do you need "-cp"? I though Java picked it up automagically from CLASSPATH (although you may need to export rather than just set).
    – paxdiablo
    Mar 3, 2009 at 5:12
0

Use %cd% to get the current directory (i.e. the one that the batch file lives in)

e.g.

set JAVA_HOME=%cd%\jdk1.x.x
set PATH=%JAVA_HOME%\bin;%PATH%
set CLASSPATH=%JAVA_HOME%\lib\tools.jar;%cd%\lib\myjar.jar;etc,etc

2
  • %cd% is not the batch file's directory, it is the current directory, e.g. I'm in C:\, I invoke C:\test\batch.bat; %cd% is C:\, not C:\test.
    – vladr
    Mar 3, 2009 at 5:04
  • @Richard - %~dp0 would get the batch file directory
    – McDowell
    Mar 3, 2009 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.