Java批处理文件 [英] Batch files for Java

查看:90
本文介绍了Java批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了使用USB磁条读取器在PC上接受信用卡,我公司在我们的Web门户上使用Java.为了使magstripe阅读器正常工作,我们的服务台必须指导客户将rxtxSerial.dll和RXTXcomm.jar文件移动到Windows中相应的bin和lib \ ext文件夹中.我们还必须在Java控制面板中的站点例外列表中添加两个URL.目前,这两个过程都涉及使用GUI并指导我们的客户在其计算机上执行此操作.

In order to use a USB magstripe reader for accepting credit cards on a PC, my company uses Java on our web portal. In order to get the magstripe reader working properly, our Helpdesk has to guide customers through moving rxtxSerial.dll and RXTXcomm.jar files to the respective bin and lib\ext folders in Windows. We also have to add two URLs to the site exceptions list in the Java Control Panel. Both of these processes currently involve using the GUI and guiding our customers through doing this on their computers.

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

Java isn't really a strong suit for me. I am trying to create a batch file (with the .dll and .jar files in the same directory) that will automate both processes so that all a user has to do is run the batch file. However, I am running into a few problems that I'm not entirely prepared for.

  1. 我需要能够将此文件用于客户计算机上Java的所有可能的安装路径.我为32位和64位Java拥有一个单独的批处理文件很好.但是,并非每个客户都在其"C:\ Program Files(x86)\ Java"目录中拥有jre8文件夹.有时,它们将代替jre8,而类似于jre1.8.0_31.我还需要批处理文件以能够解决这些差异.

  1. I need to be able to have this file work for all possible installation paths of Java on the customer's computer. I am fine having a separate batch file for 32-but and 64-bit Java. However, not every customer has the jre8 folder in their "C:\Program Files (x86)\Java" directory. Sometimes instead of jre8, they will have something like jre1.8.0_31. I need the batch file to be able to account for these differences as well.

据我所知,sites.exceptions文件存储在用户目录下的子目录中.问题是我需要能够创建一个批处理文件,该文件能够将当前用户的目录定位为完整文件路径的一部分.此外,我还不太清楚如何从命令提示符下编辑sites.exceptions.我想我可以有一个预制的例外文件,该文件会复制到正确的目录中(并自然覆盖任何当前存在的文件).

As far as my understanding goes, the sites.exceptions file is stored in a subdirectory contained within the user's directory. The problem is that I need to be able to create a batch file that will be able to locate the current user's directory as a part of the full file path. Furthermore, I'm not entirely clear on how to edit the sites.exceptions from the command prompt. I suppose I could have a pre-made exceptions file that gets copied over the the right directory (and naturally overriding any currently existing file).

我对IT领域非常陌生,所以我不知道我要问的内容是否可行,或者我是否正在解释这一权利.我真的很感谢您的帮助.

I'm very new to the IT field, so I don't know if what I am asking is possible or if I'm explaining this right. I would really appreciate some help.

推荐答案

我认为这可能对您有所帮助.该程序检测是否安装了Java.它还可以检测出哪个版本的Java等.

I think this might help you. This program detects wheter Java is installed or not. It also detects which version of java etc.

    @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执行数据包了.我们可以使用java trough cmd,所以这应该很容易.

Now we've found the java HomeDirectory we can execute our packets trough java. We can use java trough cmd, so this should need to be easy.

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

Example of what we can do now:

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

这是主要代码,它使您可以运行Java Applet/程序

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浏览了此脚本 这是完整的脚本:

I borowed this script from CATALINA This is the full script:

INGORE

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批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆