Spaces in file path in java

2019-08-20 10:17发布

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.

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.

If I give without space ie C:\Trial_Library\jar_Folder\ then it works fine.

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

3条回答
你好瞎i
2楼-- · 2019-08-20 10:31

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.

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 pb = new ProcessBuilder("myBatchFile.bar", path);
查看更多
Rolldiameter
3楼-- · 2019-08-20 10:33

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.

查看更多
Anthone
4楼-- · 2019-08-20 10:46

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.

For ex:

**Original PATH : C:/Program Files/Jdk1.6.0_07/bin

PATH to be used : 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.

查看更多
登录 后发表回答