# Batch file execution problem



## Rickiss (May 15, 2008)

*@echo off
pushd %CD%
cls
set jpath=C:\Program Files\Java\jdk1.6.0_07\bin
cd /d %jpath%
if EXIST "javac.exe" goto FOUND
if NOT EXIST "javac.exe" goto NOTFOUND
:NOTFOUND
cls
echo JAVAC.EXE FILE NOT FOUND
pause
goto END
:FOUND
echo JAVAC.EXE FILE FOUND
echo.
pause
popd
:END*

:4-dontkno

I have written a .bat code of setting path for JAVA complier but it closes automatically after execution. How to retain the cmd instance for further working. 
I'm fed up b'coz i have to do everything manually again even executing the .bat file.
Plz help me out.

Thanx a lot in advance.


----------



## jamiemac2005 (Jul 5, 2007)

To see the result of the batch file you should run it from a command prompt rather than just double clicking from explorer... why don't you just add java's "bin" directory to the path environment variable?

I assume you're doing this so after the script has run you can compile a .java file?(If i'm completely wrong please do post with the actual reason you're doing it)
You could also add an "autocompile" like entry to the .java file context menu(If you want more info on this i can go into it in more detail)...

Cheers =]
Jamey


----------



## jamiemac2005 (Jul 5, 2007)

oh p.s. i'm not sure if the directory you "cd" into will remain in the command prompt if you run it from the command prompt... 
you could change 

```
cd /d %jpath%
```
into

```
cmd.exe %jpath%
```
(or just cmd %jpath%)
to open a new instance of the command prompt at that directory?


----------



## jamiemac2005 (Jul 5, 2007)

Hey again, i've done some reading =] cmd.exe /? brings up the command line parameters for cmd.exe and if you run:

```
cmd /k "[path to your batch file]"
e.g. cmd /k "D:\Command-line Params\cmd\test with k param.bat"
```
(/k meaning keep or keep open)

the command prompt stays open after the .bat file has run (yet again this could be added to the .bat context menu)... oh and the path does remain as it's changed in the bat file...

Cheers,
Jamey


----------



## Rickiss (May 15, 2008)

jamiemac2005 said:


> Hey again, i've done some reading =] cmd.exe /? brings up the command line parameters for cmd.exe and if you run:
> 
> ```
> cmd /k "[path to your batch file]"
> ...



Jamey,
You mean to say that I have to create a new bat file for this purpose. Is it not possible to do everything in the same .bat file.


----------



## Rickiss (May 15, 2008)

jamiemac2005 said:


> Hey again, i've done some reading =] cmd.exe /? brings up the command line parameters for cmd.exe and if you run:
> 
> ```
> cmd /k "[path to your batch file]"
> ...



Jamey, acc. to your saying, I've edited my code but i still have a problem with this.
Now, this time I've to call the .bat file itself after executing the same file. Then i have to exit from it for further processing. I know there's very little error. 
Can you help me in this matter. Here is the code...


*@echo off
pushd %CD%
cmd /k
cls
set jpath=C:\Program Files\Java\jdk1.6.0_07\bin
cd /d "%jpath%"
if EXIST "javac.exe" goto FOUND
if NOT EXIST "javac.exe" goto NOTFOUND
:NOTFOUND
cls
echo JAVAC.EXE FILE NOT FOUND
pause
goto END
:FOUND
echo JAVAC.EXE FILE FOUND
echo.
pause
popd
:END*


----------



## jamiemac2005 (Jul 5, 2007)

Hey, i had a few problems trying to work this out...

Basically i understand why your code isn't working(because the batch file waits for an exit code before beginning to run again)... but not how to fix it...

If you had another batch file launching this file it would work (but i understand you want it in one batch file)... So i came up with this:


```
@echo off
if [%1] == [keptOpen] goto RUNSCRIPT
cmd /k "test.bat keptOpen"
goto END
:RUNSCRIPT
pushd %CD%
cls
set jpath=C:\Program Files\Java\jdk1.6.0_07\bin
cd /d %jpath%
if EXIST "javac.exe" goto FOUND
if NOT EXIST "javac.exe" goto NOTFOUND
:NOTFOUND
cls
echo JAVAC.EXE FILE NOT FOUND
pause
goto END
:FOUND
echo JAVAC.EXE FILE FOUND
echo.
pause
popd
:END
```
i got it working for me, and i'm pretty sure it's self explanatory? Let me know how you get on with it.

Cheers,
Jamey


----------



## jamiemac2005 (Jul 5, 2007)

oh p.s. the string test.bat is what that batch file was named on my local pc so it would need changing on yours to whatever it is that you've called it.


----------



## Rickiss (May 15, 2008)

Jamey,

Thanx for your support but the code is still not working for me. Now the cmd window will kept open and I got the msg that JAVAC.exe found but when i execute the javac then again...
Javac.exe is not recognized as internal or external command.
It seems that .bat code is unable in setting path.

Thx again Jamey for your support.


----------



## jamiemac2005 (Jul 5, 2007)

Hey, it's because you popd the beginning directory, once the script is run use %jpath%\javac.exe to run javac or try this code:


```
@echo off
if [%1] == [keptOpen] goto RUNSCRIPT
cmd /k "test.bat keptOpen"
goto END
:RUNSCRIPT
pushd %CD%
cls
set jpath=C:\Program Files\Java\jdk1.6.0_07\bin
cd /d %jpath%
if EXIST "javac.exe" goto FOUND
if NOT EXIST "javac.exe" goto NOTFOUND
:NOTFOUND
cls
echo JAVAC.EXE FILE NOT FOUND
pause
goto END
:FOUND
echo JAVAC.EXE FILE FOUND
echo.
[b]rem the following line sets the path environment variable to include the jpath variable
set PATH=%jpath%;%PATH%[/b]
pause
popd
:END
```
The above code sets the PATH environment variable for the instance of the command prompt, ensure if you change the code that %PATH% is always appended somewhere so that you don't lose your path variable(which i think would be temporary but you can never be too careful).

Cheers,
Jamey


----------



## Rickiss (May 15, 2008)

Thanx Jamey,

You have solved my problem. Now everything is going my way. You are a genius man. 
Great. THANX :wave:

:smile:


----------



## jamiemac2005 (Jul 5, 2007)

Thats k =] Thx to you too, i haven't played around with batch files in a big way but this gave me some more experience of them...

Cheers,
Jamey =]


----------

