java java中文件路径中的空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12191302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spaces in file path in java
提问by Prabhu RK
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.
我有一个包含几个 jar 文件的文件夹。我指的是来自其他位置的另一个 jar 文件的那些 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.
我的问题是,当我像这样给出 jar 文件夹的路径时C:\Trial Library\jar Folder\
。如果文件夹名称(试用库)中有空格,则无法找到此文件夹。
If I give without space ie C:\Trial_Library\jar_Folder\
then it works fine.
如果我没有空间即给予,C:\Trial_Library\jar_Folder\
那么它工作正常。
Please help me to fix this issue ASAP.
请帮助我尽快解决这个问题。
Here is my Batch File
这是我的批处理文件
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"));
}
Thanx in advance
提前谢谢
Regards, Prabhu
问候, 帕布
回答by MadProgrammer
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
This won't work. Basically, your batch file now thinks it has 5 parameters instead of one.
这行不通。基本上,您的批处理文件现在认为它有 5 个参数而不是一个。
To fix the problem you need to pass each command/parameter seperatly...
要解决此问题,您需要单独传递每个命令/参数...
Runtime.exec(new String[] {"mybatchFile.bat", path});
Or better still, use ProcessBuilder
或者更好的是,使用 ProcessBuilder
ProcessBuilder pb = new ProcessBuilder("myBatchFile.bar", path);
回答by imulsion
Wrap the path in quotes. This means that the computer takes it literally. You can have a similar problem with notepad, where it adds a .txt
extension on the end even if you supply the extension. Wrapping in quotes solves this problem.
将路径用引号括起来。这意味着计算机从字面上理解它。记事本可能会遇到类似的问题,.txt
即使您提供扩展名,它也会在末尾添加扩展名。用引号括起来解决了这个问题。
回答by Deepesh Kankaria
Generally enclosing the path in double quotes ("path") works on platforms like Unix, Linux etc. The problem only comes on WIN platform. The reason behind this is that as soon as the WIN sees a space in the path of a file to be executed, it reverts to 8.3 naming. In this naming, it takes the first 6 characters of the Sub directory as the param and searches for the pattern. To solve the issue, you have to append the first 6 characters with a tilde(~) and a number representing the instance of that pattern.
通常将路径括在双引号(“路径”)中适用于 Unix、Linux 等平台。问题仅出现在 WIN 平台上。这背后的原因是,一旦 WIN 在要执行的文件的路径中看到空格,它就会恢复为 8.3 命名。在这个命名中,它以子目录的前6个字符作为参数并搜索模式。要解决此问题,您必须在前 6 个字符后附加波浪号 (~) 和一个代表该模式实例的数字。
For ex:
例如:
**Original PATH : C:/Program Files/Jdk1.6.0_07/bin
**原始路径:C:/Program Files/Jdk1.6.0_07/bin
PATH to be used : C:/Progra~1/Jdk1.6.0_07/bin**
要使用的路径:C:/Progra~1/Jdk1.6.0_07/bin**
I have used the similar approach in lots of my Java applications and it has worked correctly all of the times.
我在很多 Java 应用程序中都使用了类似的方法,并且它一直都能正常工作。