如何编写批处理文件以设置类路径并执行Java程序 [英] How to write a batch file to set classpath and execute java programs

查看:111
本文介绍了如何编写批处理文件以设置类路径并执行Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的某些Java程序需要那么多的jar文件才能执行.为了执行此操作,我可能必须在环境变量的classpath变量中添加所有这些jar文件,否则每次打开新的cmd提示符时,都必须在命令提示符下手动设置classpath.我不想在环境变量的classpath变量处添加所有jar文件,也不想在每次打开新的cmd提示时都进行手动设置.我想在批处理文件中编写脚本来设置类路径,并且它本身需要运行Java程序.这样,无论何时我想运行程序,我都将运行批处理文件并逐一运行程序.我已经写了一个批处理文件来设置类路径.但是,当我运行此批处理文件时,它会自动关闭.因此,我无法使用我通过批处理文件设置的类路径,再次必须打开新的cmd提示符,设置类路径并运行我的Java程序.要使用批处理文件实现此目的,我该如何进行.感谢任何帮助.谢谢.

Some of my java programs need so many jar files to execute. For executing this, I may have to add all of those jar files in classpath variable of environment variables or else have to set classpath manually at command prompt each and every time when I open a new cmd prompt. I do not want to add all jar files at classpath variable in environment variables and also set manually each and every time when I open new cmd prompt. I would like to write a script in batch file to set classpath and there it self need to run java programs. So that, when ever I want to run my programs, I will just run batch file and run my programs one by one. I have written a batch file to set classpath. But, when I run this batch file, it automatically get closed. So, Am not able to utilize the classpath I set by batch file.Again I had to open new cmd prompt, set classpath and run my java programs. To achieve this with batch file, how could I proceed. Appreciate any help. Thank you.

推荐答案

不需要批处理文件在命令行上将Java的类路径指定为

There is no need for a batch file to specify the classpath for Java on command line as Jack wrote in his comment.

看看版本7 Java文档页面:

Take a look on the version 7 Java documentation pages:

  • Java
  • Setting the class path

人类-classpath具有-cp或更好的可读性,可以在命令行上用于定义 classpath .

There is -cp or better readable for humans -classpath which can be used on command line to define the classpath.

可以使用分号作为分隔符来指定多个类的路径.

The paths to multiple classes can be specified by using a semi-colon as separator.

如果一个路径包含空格字符,则必须在所有路径周围使用双引号.

And double quotes around all paths must be used if one path contains a space character.

示例:

"%JAVA_HOME%\bin\java.exe" -classpath "C:\Java\My First Class;C:\Java\MySecondClass" MyJavaApp

此方法主要用于对Java应用程序使用快捷方式(* .lnk),该快捷方式需要与系统范围环境变量 CLASSPATH 中通常使用和定义的类不同的类.

This approach is mainly useful on using a shortcut (*.lnk) to a Java application which needs different classses than what is usually used and defined in system wide environment variable CLASSPATH.

但是,如果要在控制台窗口中使用与定义的系统范围不同的类列表来开发和测试Java应用程序,最好有一个批处理文件,例如名称为JavaDevEnv.bat并带有以下代码

But for developing and testing Java applications in a console window with a different list of classes than defined system wide, it is better to have a batch file for example with name JavaDevEnv.bat with following code

@echo off
title Java Development Environment
cd /D "Path to\Preferred Working Directory\For\Java\Development"
set "CLASSPATH=C:\Java\My First Class;C:\Java\MySecondClass"

并使用命令行在Windows桌面或Windows开始"菜单中创建快捷方式

and create a shortcut on Windows desktop or in Windows start menu with the command line

%SystemRoot%\System32\cmd.exe /K "Path to\Batch File\JavaDevEnv.bat"

在快捷方式文件(* .lnk)的属性中定义.

defined in the properties of the shortcut file (*.lnk).

也可以使用快捷方式文件属性中的开始于来定义工作目录,而不用使用更改目录命令在批处理文件中进行设置.

The working directory can be also defined with Start in in properties of the shortcut file instead of being set in batch file using change directory command.

并且还应在快捷方式文件的属性中编写适当的 Comment ,例如,与在命令title上使用的属性相同,该命令将控制台窗口的标题设置为提示,以供使用此快捷方式.

And an appropriate Comment should be also written in properties of the shortcut file, for example same as used on command title which sets the title of the console window as hint for which purpose to use this shortcut.

双击此快捷方式将打开一个新的控制台窗口,执行该批处理文件,该文件设置窗口标题,工作目录和环境变量 CLASSPATH ,该Java变量是从该控制台窗口中执行的,然后由Java执行保持开放供用户输入.

A double click on this shortcut results in opening a new console window, executing the batch file which sets window title, working directory and environment variable CLASSPATH used by Java executed from within this console window and then remains open for user input.

这篇关于如何编写批处理文件以设置类路径并执行Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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