gpt4 book ai didi

java - Java 的批处理文件

转载 作者:太空宇宙 更新时间:2023-11-04 14:06:54 24 4
gpt4 key购买 nike

为了使用 USB 磁条阅读器在 PC 上接受信用卡,我公司在我们的门户网站上使用 Java。为了使磁条阅读器正常工作,我们的帮助台必须指导客户将 rxtxSerial.dll 和 RXTXcomm.jar 文件移动到 Windows 中相应的 bin 和 lib\ext 文件夹。我们还必须将两个 URL 添加到 Java 控制面板中的站点异常(exception)列表中。目前,这两个流程都涉及使用 GUI 并指导我们的客户在他们的计算机上完成此操作。

Java 对我来说并不是真正的强项。我正在尝试创建一个批处理文件(.dll 和 .jar 文件位于同一目录中),该文件将自动执行这两个过程,以便用户所要做的就是运行批处理文件。然而,我遇到了一些我还没有完全准备好的问题。

  1. 我需要能够让该文件适用于客户计算机上所有可能的 Java 安装路径。我可以为 32 位和 64 位 Java 提供单独的批处理文件。但是,并非每个客户的“C:\Program Files (x86)\Java”目录中都有 jre8 文件夹。有时,他们会使用类似 jre1.8.0_31 的东西来代替 jre8。我也需要批处理文件来解释这些差异。

  2. 据我了解,sites.exceptions 文件存储在用户目录中包含的子目录中。问题是我需要能够创建一个批处理文件,该文件能够将当前用户的目录定位为完整文件路径的一部分。此外,我并不完全清楚如何从命令提示符编辑sites.exceptions。我想我可以有一个预先制作的异常文件,该文件可以复制到正确的目录(并且自然地覆盖任何当前现有的文件)。

我对 IT 领域很陌生,所以我不知道我的要求是否可行,或者我的解释是否正确。我真的很感激一些帮助。

最佳答案

我想这可能对你有帮助。该程序检测是否安装了 Java。它还检测 java 的版本等。

    @echo off
if "%OS%" == "Windows_NT" setlocal
::::::::::::::::::::::::::::::::::::
:: Set JAVA_HOME or JRE_HOME ::
::::::::::::::::::::::::::::::::::::
set JDKKeyName64=HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit
set JDKKeyName32=HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Development Kit
set JREKeyName64=HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment
set JREKeyName32=HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment

reg query "%JDKKeyName64%" /s
if %ERRORLEVEL% EQU 1 (
echo .Could not find 32 bit or 64 bit JDK
echo .Looking for 32 bit JDK on 64 bit machine
goto FINDJDK32
)
set KeyName=%JDKKeyName64%
goto JDKRUN

:FINDJDK32
reg query "%JDKKeyName32%" /s
if %ERRORLEVEL% EQU 1 (
echo .Could not find 32 bit JDK
echo .Looking for 32 bit or 64 bit JRE
goto FINDJRE64
)
set KeyName=%JDKKeyName32%
goto JDKRUN

:FINDJRE64
reg query "%JREKeyName64%" /s
if %ERRORLEVEL% EQU 1 (
echo .Could not find 32 bit or 64 bit JRE
echo .Looking for 32 bit JRE on 64 bit machine
goto FINDJRE32
)
set KeyName=%JREKeyName64%
goto JRERUN

:FINDJRE32
reg query "%JREKeyName32%" /s
if %ERRORLEVEL% EQU 1 (
echo .Could not find 32 bit JRE
echo .Could not set JAVA_HOME or JRE_HOME. Aborting
goto ENDERROR
)
set KeyName=%JREKeyName32%
goto JRERUN

:JDKRUN
echo.
echo Using JDK
set "CURRENT_DIR=%cd%"
set "YOUR_PROGRAM_HOME_HERE=%CURRENT_DIR%"

set Cmd=reg query "%KeyName%" /s
for /f "tokens=2*" %%i in ('%Cmd% ^| find "JavaHome"') do set JAVA_HOME=%%j

echo.
echo Seems fine!
echo Set JAVA_HOME : %JAVA_HOME%
echo Set YOUR_PROGRAM_HOME_HERE : %YOUR_PROGRAM_HOME_HERE%
echo.

goto NEXT

:JRERUN
echo.
echo [XAMPP]: Using JRE
set "CURRENT_DIR=%cd%"
set "YOUR_PROGRAM_HOME_HERE=%CURRENT_DIR%\tomcat"

set Cmd=reg query "%KeyName%" /s
for /f "tokens=2*" %%i in ('%Cmd% ^| find "JavaHome"') do set JRE_HOME=%%j

echo.
echo Seems fine!
echo Set JRE_HOME : %JRE_HOME%
echo Set YOUR_PROGRAM_HOME_HERE : %CATALINA_HOME%
echo.

:ENDERROR
exit 1

:NEXT

echo Finding Java Version

set Cmd=reg query "%KeyName%" /v CurrentVersion
for /f "tokens=2*" %%i in ('%Cmd% ^| find "CurrentVersion"') do set CVERSION=%%j

echo Java Version: %CVERSION%
echo Starting Service Install...
echo .

rem ----------END JAVA SEARCH-----------------------------------------------------------------

现在我们已经找到了 java HomeDirectory,我们可以通过 java 执行我们的数据包。我们可以通过 cmd 使用 java,所以这应该很容易。

我们现在可以做什么的示例:

rem Make sure prerequisite environment variables are set
if not "%JAVA_HOME%" == "" goto gotJdkHome
if not "%JRE_HOME%" == "" goto gotJreHome
echo Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
echo Service will try to guess them from the registry.
goto runAsUser
:gotJreHome
if not exist "%JRE_HOME%\bin\java.exe" goto noJavaHome
if not exist "%JRE_HOME%\bin\javaw.exe" goto noJavaHome
goto runAsUser
:gotJdkHome
if not exist "%JAVA_HOME%\jre\bin\java.exe" goto noJavaHome
if not exist "%JAVA_HOME%\jre\bin\javaw.exe" goto noJavaHome
if not exist "%JAVA_HOME%\bin\javac.exe" goto noJavaHome
if not "%JRE_HOME%" == "" goto runAsUser
set "JRE_HOME=%JAVA_HOME%\jre"
goto runAsUser
:noJavaHome
echo The JAVA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
echo NB: JAVA_HOME should point to a JDK not a JRE
goto end

Here comes the main code that let's you run your java applet/program

:doInstall
rem Install the service
echo Installing the service '%SERVICE_NAME%' ...
echo Using PROGRAM_HOME: "%YOUR_PROGRAM_HOME_HERE%"
echo Using JAVA_HOME: "%JAVA_HOME%"
echo Using JRE_HOME: "%JRE_HOME%"

rem Use the environment variables as an example
rem Each command line option is prefixed with PR_

set "PR_INSTALL=%EXECUTABLE%"

rem Add another jar or java file next to it with a semicolon. (still in the quotes)

set "PR_CLASSPATH=%YOUR_PROGRAM_HOME_HERE%\javawishprogram.jar;%YOUR_PROGRAM_HOME_HERE%\MaybeASecondoneIfYouwant.jar"
rem Set the server jvm from JAVA_HOME
set "PR_JVM=%JRE_HOME%\bin\server\jvm.dll"

rem Here you can go further on the JVM or not, you can map out your options.
if exist "%PR_JVM%" goto foundJvm

rem Set the client jvm from JAVA_HOME
set "PR_JVM=%JRE_HOME%\bin\client\jvm.dll"
if exist "%PR_JVM%" goto foundJvm
set PR_JVM=auto
:foundJvm
echo Using JVM: "%PR_JVM%"
rem Here you're launching the main programme.
"%EXECUTABLE%" //IS//%SERVICE_NAME% --StartClass programJava.startup.Bootstrap --StartParams start --StopParams stop --Startup auto

You can do even more with this. But I'll let it to your imagination.

这个脚本是我从 CATALINA 借来的这是完整的脚本:

INGORE

@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements. See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License. You may obtain a copy of the License at
rem
rem http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.

if "%OS%" == "Windows_NT" setlocal
rem ---------------------------------------------------------------------------
rem NT Service Install/Uninstall script
rem
rem Options
rem install Install the service using Tomcat7 as service name.
rem Service is installed using default settings.
rem remove Remove the service from the System.
rem
rem name (optional) If the second argument is present it is considered
rem to be new service name
rem
rem $Id: service.bat 1000718 2010-09-24 06:00:00Z mturk $
rem ---------------------------------------------------------------------------

rem ---------XAMPP-------------------------------------------------------------
::::::::::::::::::::::::::::::::::::
:: Set JAVA_HOME or JRE_HOME ::
::::::::::::::::::::::::::::::::::::

echo.
echo [XAMPP]: Searching for JDK or JRE HOME with reg query ...
set JDKKeyName64=HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit
set JDKKeyName32=HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Development Kit
set JREKeyName64=HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment
set JREKeyName32=HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment

reg query "%JDKKeyName64%" /s
if %ERRORLEVEL% EQU 1 (
echo . [XAMPP]: Could not find 32 bit or 64 bit JDK
echo . [XAMPP]: Looking for 32 bit JDK on 64 bit machine
goto FINDJDK32
)
set KeyName=%JDKKeyName64%
goto JDKRUN

:FINDJDK32
reg query "%JDKKeyName32%" /s
if %ERRORLEVEL% EQU 1 (
echo . [XAMPP]: Could not find 32 bit JDK
echo . [XAMPP]: Looking for 32 bit or 64 bit JRE
goto FINDJRE64
)
set KeyName=%JDKKeyName32%
goto JDKRUN

:FINDJRE64
reg query "%JREKeyName64%" /s
if %ERRORLEVEL% EQU 1 (
echo . [XAMPP]: Could not find 32 bit or 64 bit JRE
echo . [XAMPP]: Looking for 32 bit JRE on 64 bit machine
goto FINDJRE32
)
set KeyName=%JREKeyName64%
goto JRERUN

:FINDJRE32
reg query "%JREKeyName32%" /s
if %ERRORLEVEL% EQU 1 (
echo . [XAMPP]: Could not find 32 bit JRE
echo . [XAMPP]: Could not set JAVA_HOME or JRE_HOME. Aborting
goto ENDERROR
)
set KeyName=%JREKeyName32%
goto JRERUN

:JDKRUN
echo.
echo [XAMPP]: Using JDK
set "CURRENT_DIR=%cd%"
set "CATALINA_HOME=%CURRENT_DIR%\tomcat"

set Cmd=reg query "%KeyName%" /s
for /f "tokens=2*" %%i in ('%Cmd% ^| find "JavaHome"') do set JAVA_HOME=%%j

echo.
echo [XAMPP]: Seems fine!
echo [XAMPP]: Set JAVA_HOME : %JAVA_HOME%
echo [XAMPP]: Set CATALINA_HOME : %CATALINA_HOME%
echo.

goto NEXT

:JRERUN
echo.
echo [XAMPP]: Using JRE
set "CURRENT_DIR=%cd%"
set "CATALINA_HOME=%CURRENT_DIR%\tomcat"

set Cmd=reg query "%KeyName%" /s
for /f "tokens=2*" %%i in ('%Cmd% ^| find "JavaHome"') do set JRE_HOME=%%j

echo.
echo [XAMPP]: Seems fine!
echo [XAMPP]: Set JRE_HOME : %JRE_HOME%
echo [XAMPP]: Set CATALINA_HOME : %CATALINA_HOME%
echo.

:ENDERROR
exit 1

:NEXT

echo [XAMPP]: Finding Java Version

set Cmd=reg query "%KeyName%" /v CurrentVersion
for /f "tokens=2*" %%i in ('%Cmd% ^| find "CurrentVersion"') do set CVERSION=%%j

echo [XAMPP]: Java Version: %CVERSION%
echo [XAMPP]: Starting Tomcat Service Install...
echo .

rem ----------END XAMPP-----------------------------------------------------------------

set "SELF=%~dp0%service.bat"
rem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%"
if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%cd%"
if exist "%CATALINA_HOME%\bin\tomcat7.exe" goto okHome
rem CD to the upper dir
cd ..
set "CATALINA_HOME=%cd%"
:gotHome
if exist "%CATALINA_HOME%\bin\tomcat7.exe" goto okHome
echo The tomcat.exe was not found...
echo The CATALINA_HOME environment variable is not defined correctly.
echo This environment variable is needed to run this program
goto end
:okHome
rem Make sure prerequisite environment variables are set
if not "%JAVA_HOME%" == "" goto gotJdkHome
if not "%JRE_HOME%" == "" goto gotJreHome
echo Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
echo Service will try to guess them from the registry.
goto okJavaHome
:gotJreHome
if not exist "%JRE_HOME%\bin\java.exe" goto noJavaHome
if not exist "%JRE_HOME%\bin\javaw.exe" goto noJavaHome
goto okJavaHome
:gotJdkHome
if not exist "%JAVA_HOME%\jre\bin\java.exe" goto noJavaHome
if not exist "%JAVA_HOME%\jre\bin\javaw.exe" goto noJavaHome
if not exist "%JAVA_HOME%\bin\javac.exe" goto noJavaHome
if not "%JRE_HOME%" == "" goto okJavaHome
set "JRE_HOME=%JAVA_HOME%\jre"
goto okJavaHome
:noJavaHome
echo The JAVA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
echo NB: JAVA_HOME should point to a JDK not a JRE
goto end
:okJavaHome
if not "%CATALINA_BASE%" == "" goto gotBase
set "CATALINA_BASE=%CATALINA_HOME%"
:gotBase

set "EXECUTABLE=%CATALINA_HOME%\bin\tomcat7.exe"

rem Set default Service name
set SERVICE_NAME=Tomcat7
set PR_DISPLAYNAME=Apache Tomcat 7

if "x%1x" == "xx" goto displayUsage
set SERVICE_CMD=%1
shift
if "x%1x" == "xx" goto checkServiceCmd
:checkUser
if "x%1x" == "x/userx" goto runAsUser
if "x%1x" == "x--userx" goto runAsUser
set SERVICE_NAME=%1
set PR_DISPLAYNAME=Apache Tomcat %1
shift
if "x%1x" == "xx" goto checkServiceCmd
goto checkUser
:runAsUser
shift
if "x%1x" == "xx" goto displayUsage
set SERVICE_USER=%1
shift
runas /env /savecred /user:%SERVICE_USER% "%COMSPEC% /K \"%SELF%\" %SERVICE_CMD% %SERVICE_NAME%"
goto end
:checkServiceCmd
if /i %SERVICE_CMD% == install goto doInstall
if /i %SERVICE_CMD% == remove goto doRemove
if /i %SERVICE_CMD% == uninstall goto doRemove
echo Unknown parameter "%1"
:displayUsage
echo.
echo Usage: service.bat install/remove [service_name] [/user username]
goto end

:doRemove
rem Remove the service
"%EXECUTABLE%" //DS//%SERVICE_NAME%
if not errorlevel 1 goto removed
echo Failed removing '%SERVICE_NAME%' service
goto end
:removed
echo The service '%SERVICE_NAME%' has been removed
goto end

:doInstall
rem Install the service
echo Installing the service '%SERVICE_NAME%' ...
echo Using CATALINA_HOME: "%CATALINA_HOME%"
echo Using CATALINA_BASE: "%CATALINA_BASE%"
echo Using JAVA_HOME: "%JAVA_HOME%"
echo Using JRE_HOME: "%JRE_HOME%"

rem Use the environment variables as an example
rem Each command line option is prefixed with PR_

set PR_DESCRIPTION=Apache Tomcat 7.0.22 Server - http://tomcat.apache.org/
set "PR_INSTALL=%EXECUTABLE%"
set "PR_LOGPATH=%CATALINA_BASE%\logs"
set "PR_CLASSPATH=%CATALINA_HOME%\bin\bootstrap.jar;%CATALINA_BASE%\bin\tomcat-juli.jar;%CATALINA_HOME%\bin\tomcat-juli.jar"
rem Set the server jvm from JAVA_HOME
set "PR_JVM=%JRE_HOME%\bin\server\jvm.dll"
if exist "%PR_JVM%" goto foundJvm
rem Set the client jvm from JAVA_HOME
set "PR_JVM=%JRE_HOME%\bin\client\jvm.dll"
if exist "%PR_JVM%" goto foundJvm
set PR_JVM=auto
:foundJvm
echo Using JVM: "%PR_JVM%"
"%EXECUTABLE%" //IS//%SERVICE_NAME% --StartClass org.apache.catalina.startup.Bootstrap --StopClass org.apache.catalina.startup.Bootstrap --StartParams start --StopParams stop --Startup auto
if not errorlevel 1 goto installed
echo Failed installing '%SERVICE_NAME%' service
goto ENDERROR
:installed
rem Clear the environment variables. They are not needed any more.
set PR_DISPLAYNAME=
set PR_DESCRIPTION=
set PR_INSTALL=
set PR_LOGPATH=
set PR_CLASSPATH=
set PR_JVM=
rem Set extra parameters
"%EXECUTABLE%" //US//%SERVICE_NAME% --JvmOptions "-Dcatalina.base=%CATALINA_BASE%;-Dcatalina.home=%CATALINA_HOME%;-Djava.endorsed.dirs=%CATALINA_HOME%\endorsed" --StartMode jvm --StopMode jvm
rem More extra parameters
set "PR_LOGPATH=%CATALINA_BASE%\logs"
set PR_STDOUTPUT=auto
set PR_STDERROR=auto

rem XAMPP: We need special parameters for Java 7
if "%CVERSION%" == "1.7" goto JAVA7
:JAVA
"%EXECUTABLE%" //US//%SERVICE_NAME% ++JvmOptions "-Djava.io.tmpdir=%CATALINA_BASE%\temp;-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager;-Djava.util.logging.config.file=%CATALINA_BASE%\conf\logging.properties" --JvmMs 128 --JvmMx 256
goto FINISH
:JAVA7
"%EXECUTABLE%" //US//%SERVICE_NAME% ++JvmOptions "-Djava.io.tmpdir=%CATALINA_BASE%\temp;-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager;-Djava.util.logging.config.file=%CATALINA_BASE%\conf\logging.properties;-Djava.net.preferIPv4Stack=true" --JvmMs 128 --JvmMx 256
:FINISH
echo The service '%SERVICE_NAME%' has been installed.

:end
cd "%CURRENT_DIR%"

关于java - Java 的批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28779561/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com