I want to set the JAVA_HOME variable from a batch script
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
You can use values stored in the registry to automatically discover where Java is installed and setup the JAVA_HOME variable.
At this location is a Key called CurrentVersion. This version references one of the directories at this level by name. Opening the directory exposes another key called JavaHome. The value of JavaHome is a file system path that can be used to define your environment variable JAVA_HOME.
Within a batch file you can do something like:
If you want to read more, I've written a tutorial describing what is needed to construct a batch file to auto-discover JAVA_HOME.
If JAVA_HOME isn't already set, and you want to set it, then you probably need to do something like
which will give you a list of all directories containing the file java.exe. From there you can pipe that output to another command that parses the results and selects one of those directories, then uses it to set the JAVA_HOME variable.
This solution depends on the JDK being installed under %ProgramFiles%\Java, for example C:\Program Files\Java\jdk1.6.0_18. You can change the line "set JDK_Version=1.6" to the version you want to use such as "set JDK_Version=1.5".
Assuming that the latest version of JDK would be at the bottom of the list (jdk%jdk_Version%*) the latest version available should be set as JAVA_HOME. If the JDK could not be found JAVA_HOME will not be changed. If the JDK could not be found and JAVA_HOME doesn't have a value the script will display an error message.
Here is a handy little batch file that will search everything in your path for the first occurance of a file. The magical little bit is the %%~dp$PATH:i%%i in the for loop. %%~dp will expand the next variable to the drive letter and path only. The $PATH:i searches the path for the first occurance of %%i which is the filename that is passed into the for loop.
example: Where.bat
See Get the current Java version from a BAT file to get the current Java installation based on the infos stored in the registry.
Java's supposed to set up the JAVA_HOME variable when it's installed. Use this to find the location of Java: