Java文件路径中的空格 [英] Spaces in file path in java
问题描述
我有一个包含几个jar文件的文件夹.我指的是来自其他位置的另一个jar文件中的那些jar文件.
I have a folder which contains few jar files. I am referring to those jar files from another jar file which is in some other location.
我的问题是,当我给出像 C:\ Trial Library \ jar Folder \
这样的jar文件夹的路径时.文件夹名称(试用库)中有空格,则无法找到此文件夹.
My problem is, when I give the path of the jar folder like this C:\Trial Library\jar Folder\
ie. with space in the folder names (Trial Library) then it is unable to locate this folder.
如果我没有空格,即 C:\ Trial_Library \ jar_Folder \
,那么它可以正常工作.
If I give without space ie C:\Trial_Library\jar_Folder\
then it works fine.
请帮助我尽快解决此问题.
Please help me to fix this issue ASAP.
这是我的批处理文件
set CURRENT_DIRECTORY=%~dp0
set ANT_HOME=%"CURRENT_DIRECTORY"%ant\apache-ant-1.8.3
ECHO current directory is %CURRENT_DIRECTORY%
ECHO %ANT_HOME%
set Path=%ANT_HOME%\bin
set ADAPTER_LIBRAY_PATH=%1
set USER_JAR_PATH=%2
set CLASS_NAME=%3
set RESULTS_PATH=%4
set JUNIT_PATH=%"CURRENT_DIRECTORY"%ANT\test\junit-4.1.jar
set LIBRAIES_TO_INCLUDE="%JUNIT_PATH%";"%ADAPTER_LIBRAY_PATH%";"%USER_JAR_PATH%"
ECHO %LIBRAIES_TO_INCLUDE%
ECHO %ADAPTER_LIBRAY_PATH%
ECHO %JUNIT_PATH%
ECHO %USER_JAR_PATH%
ECHO %CLASS_NAME%
ECHO %RESULTS_PATH%
ant -lib "%LIBRAIES_TO_INCLUDE%" -Dlibraries="%ADAPTER_LIBRAY_PATH%" -Djunitlibrary="%JUNIT_PATH%" -Djartobeexec="%USER_JAR_PATH%" -Duserclass=%CLASS_NAME% -Dresultspath=%RESULTS_PATH% -buildfile build.xml test-html
在这里我将值传递到批处理文件中
Here is where i pass the values to my batch file
String[] commands=new String[5];
commands[0]="driver.bat";
commands[1]=finalLibraryPath;
commands[2]=executingJarLocation;
commands[3]=tempPackageName;
commands[4]=resultsFolderPath;
process = Runtime.getRuntime().exec(commands);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuffer errorStr = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
errorStr.append(line);
errorStr.append(System.getProperty("line.separator"));
}
先感谢
关于,普拉布(Prabhu)
Regards, Prabhu
推荐答案
好的,据我了解,我正在猜测"您正在做类似的事情
Okay, from what I understand I'm "guessing" that you are doing something like
Runtime.exec("myBatchFile.bat " + path);
这将以眼泪结束.这相当于说:
This will end in tears. This is the equivalent of saying:
C:> myBatchFile.bat C:\Path to my jar files
这行不通.基本上,您的批处理文件现在认为它有5个参数,而不是一个.
This won't work. Basically, your batch file now thinks it has 5 parameters instead of one.
要解决此问题,您需要分别传递每个命令/参数...
To fix the problem you need to pass each command/parameter seperatly...
Runtime.exec(new String[] {"mybatchFile.bat", path});
或更妙的是,使用 ProcessBuilder
ProcessBuilder pb = new ProcessBuilder("myBatchFile.bar", path);
这篇关于Java文件路径中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!