-4

I have written bat file to get path from system variable using %JAVA_HOME% and assigned it into one variable, now how to use as set path, because in set path its asking me this path between " " code, which i am not able to add in.

Please help me to add it, how ?

Jesper
  • 186,095
  • 42
  • 296
  • 332
Sonu
  • 3
  • 3
  • who is asking for what? – tak3shi Mar 30 '17 at 06:46
  • 1
    understanding your question is one thing, understanding your problem is another thing, i'm confused – johnII Mar 30 '17 at 06:49
  • This question is not about Java, but about Windows batch files. Please show us what you've tried and what error message you get - that will make it much easier to understand what you are trying to accomplish and help you solve the problem. – Jesper Mar 30 '17 at 06:54
  • Hi, I am trying to set JAVA_CMD="C:\Program Files\Java\jdk1.8.0_92\jre\bin\java" hard coded into bat file, which is working file, but I have to set path dynamically. – Sonu Mar 30 '17 at 06:58
  • 3
    I cannot find anything about 'dynamically' in your question above. There is also no code you have done already. I have no idea what you want and i am really confused. – tak3shi Mar 30 '17 at 07:05
  • Hi Jesper, I have these code for bat file, Which is working fine. @ECHO OFF set JAVA_CMD="C:\Program Files\Java\jdk1.8.0_92\jre\bin\java" %JAVA_CMD% TransferArborDeereFiles pause now only i want to set JAVA_CMD PATH Dynamically means it can be run from any system. – Sonu Mar 30 '17 at 07:15
  • Please make your question as clear as possible! Put code you have into the question post by [editing](http://stackoverflow.com/posts/43110316/edit) it! Do *not* post code in comments, because it is hardly readable due to limited formatting capabilities, and other users had to put together the relevant portions from the comments on their own in order to understand the question, which they are likely not willing to do... – aschipfl Mar 30 '17 at 07:53

1 Answers1

0

It is a big difference if an environment variable is defined with set variable="value" or with set "variable=value". Read the answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? for details.

It is strongly recommended to use set "variable=value", i.e. assign the string to the environment variable without double quotes and reference the value of the environment variable always enclosed in double quotes.

It looks like you use:

@ECHO OFF
set JAVA_CMD="C:\Program Files\Java\jdk1.8.0_92\jre\bin\java"
%JAVA_CMD% TransferArborDeereFiles
pause

So to get path of java executable with having file extension .exe not specified and therefore Windows command interpreter needs to search for java.* with a file extension listed in local environment variable PATHEXT, the following batch code could be used:

@ECHO OFF
set JAVA_CMD="C:\Program Files\Java\jdk1.8.0_92\jre\bin\java"
rem %JAVA_CMD% TransferArborDeereFiles
for /F "delims=" %%I in (%JAVA_CMD%) do set "JAVA_CMD_PATH=%%~dpI"
set JAVA_CMD
pause

This batch file just outputs the values of the environment variables JAVA_CMD and JAVA_CMD_PATH.

But much better would be:

@ECHO OFF
setlocal EnableExtensions DisableDelayedExpansion

set "JAVA_CMD=%ProgramFiles%\Java\jdk1.8.0_92\jre\bin\java.exe"
for /F "delims=" %%I in ("%JAVA_CMD%") do set "JAVA_CMD_PATH=%%~dpI"

rem Output all environment variables starting with JAVA_CMD.
set JAVA_CMD

rem "%JAVA_CMD%" TransferArborDeereFiles
rem "%JAVA_CMD_PATH%OtherScriptOrExe" "argument 1" "argument 2"

endlocal

This batch file should be executed in a command prompt window and not with double clicking on the batch file. Debugging a batch file in development should be always done by running it from within a command prompt window to see possible error messages output by Windows command interpreter of errors resulting in exiting batch file execution.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • for /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?
Community
  • 1
  • 1
Mofi
  • 38,783
  • 14
  • 62
  • 115