[英]Compile project and include libraries using batch script
In CMD
I compile project with including libraries 在CMD
我编译包含库的项目
java -cp app.jar;libs/*;. com.app.Main
and it works, but I want create BATCH
script, which do exactly the same. 并且它可以工作,但是我想创建BATCH
脚本,其功能完全相同。 I create test.bat
and put code like below: 我创建了test.bat
并放入如下代码:
@ECHO off
java -cp app.jar;libs/*;. com.app.Main
PAUSE
But when I run the test.bat
the CMD
was shown and there is information "Error: Could not find or load main class com.app.Main".
但是,当我运行test.bat
将显示CMD
并且显示信息"Error: Could not find or load main class com.app.Main".
BATCH
script is located at the same folder as app.jar
and libs
folder. BATCH
脚本与app.jar
和libs
文件夹位于同一文件夹。
What is wrong with this batch script? 这个批处理脚本有什么问题?
Probably the characters on the compile line are significant to the batch interpreter; 编译行上的字符可能对批处理解释器很重要; try putting the classpath in double quotes. 尝试将类路径放在双引号中。
After Java 6, Classpaths could be built by using wildcard characters . 在Java 6之后,可以使用通配符来构建类路径 。
You can create a directory named classpath
and put your JARs inside it. 您可以创建一个名为classpath
的目录,并将其JAR放入其中。 Then you can create your .bat
file like this: 然后,您可以按以下方式创建.bat
文件:
@ECHO off
java -cp .;classpath/* com.app.Main
pause
You should have a structure like this: 您应该具有以下结构:
com
`---app
`---Main.java
classpath
`---your-crital-code-1.0.jar
compile.bat
I see that a lot of people are asking this question on StackOverflow, so here is a little tip for you guys. 我看到很多人在StackOverflow上问这个问题,所以这里给您一些提示。
I usually avoid using the cd
command, as it may create some hassle. 我通常避免使用cd
命令,因为它可能会带来一些麻烦。 In windows, you can Shift + Right Click
to open a command window in a particular directory. 在Windows中,您可以Shift + Right Click
以在特定目录中打开命令窗口。
I always prefer relative paths over absolutes, so that I don't get into hassle of managing long paths. 我总是喜欢相对路径而不是绝对路径,这样我就不会麻烦管理长路径。
Here is a little program that deals with this kind of problem. 这是一个处理此类问题的小程序。 You can always refer to and contribute to it so that we can make good examples for Java beginners :) 您总是可以参考它并为此做出贡献 ,以便我们为Java初学者树立良好的榜样:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.