我正在从utf-8编码的XML文件中读取文件/脚本的名称。然后将其传递给VBScript。 VBScript启动第二个程序,该传递的程序/脚本作为参数提供给该程序。当参数是英语时,VBScript成功执行。
但如果从XML读取的名称是非英语(在我的情况下是俄语),则VBScript无法找到该文件。
我正在使用“cscript”从Java代码运行VBScript,因为我在Windows上运行它。
但是,如果我复制Java程序触发的命令来运行VBScript,并将其粘贴到命令提示符上,它会正常执行,尽管参数名称是非英语语言。
然后我在VBScript中硬编码文件/脚本名称。将VBScript的编码更改为UCS2-LE,并直接从命令提示符运行它。它正常执行。它无法执行任何其他用于VBScript的编码。非英语文本也显示为?在任何其他编码而不是UCS2-LE。
然后我尝试将文件/脚本名称编码为Java中的UTF16-LE,然后将其传递给VBScript。无论在VBScript中使用哪种编码,它都会失败。再次,如果我从Java程序复制打印在标准输出上的命令并从cmd运行它,它就会执行。 从Java打印的命令正确显示非英语文本。
有人可以帮我解决这个问题吗? 任何相关的帮助将不胜感激。
这就是我目前正在做的事情。我需要将一个带有俄语文本的参数从Java传递给VBScript。
我尝试使用两种不同的方法。
下面代码中的第一种方法是使用UnicodeLittle编码将俄语文本写入文件中。发现文件在编码UCS-2LE中。然后VBScript从该文件中读取值,并成功执行脚本。
在第二种方法中,我尝试直接将编码的俄语文本作为参数传递给脚本。 VbScript无法说无法打开脚本。这是我想要解决的方法。
以下是附加的Java代码。
非常感谢任何帮助。
public class CallProgram
{
private static String encodeType = "UnicodeLittle";
private File scriptName = new File( "F:\\Trial Files\\scriptName.txt" );
public static void main(String[] args)
{
CallProgram obj = new CallProgram();
Runtime rt = Runtime.getRuntime();
try
{
**//Approach1 - Writes text to file and calls vbscript which reads text from file and uses it as an argument to a program**
String sName = "D:\\CheckPoints_SCRIPTS\\Менеджер по качеству"; //Russian Text
byte [] encodedByte= sName.getBytes( encodeType );
String testCase = new String( encodedByte, encodeType ); //New string containing russian text in UnicodeLittle encoding...
obj.writeToFile( testCase ); //Writing russian string to file...
String mainStr = "cscript /nologo \"D:\\Program Files\\2.0.1.3\\Adapter\\bin\\scriptRunner_FileRead_Write.vbs\"";
Process proc1 = rt.exec( mainStr );
int exit = proc1.waitFor();
System.out.println( "Exit Value = " + exit );
**//Approach 2 - Passing encoded Russian text directly to VbScript...**
//This is not working for me...
String [] arrArgs = { "cscript", "/nologo", "\"D:\\Program Files\\IBM\\Rational Adapters\\2.0.1.3\\QTPAdapter\\bin\\scriptRunner.vbs\"", testcase };
ProcessBuilder process = new ProcessBuilder( arrArgs );
Process proc2 = process.start();
proc2.waitFor();
}
catch (IOException e)
{
e.printStackTrace();
}
catch ( InterruptedException intue )
{
intue.printStackTrace();
}
}
//Function to write Russian text to file using encoding UnicodeLittle...
private void writeToFile( String testCase )
{
FileOutputStream fos = null;
Writer out = null;
try
{
fos = new FileOutputStream( this.scriptName );
out = new OutputStreamWriter( fos, encodeType );
out.write( testCase );
out.close();
fos.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch ( IOException ioe )
{
ioe.printStackTrace();
}
finally
{
try
{
if ( fos != null )
{
fos.close();
fos = null;
}
if ( out != null)
{
out.close();
out = null;
}
}
catch( IOException ioe )
{
fos = null;
out = null;
}
}
} // End of method writeToFile....
}
答案 0 :(得分:2)
我之前通过使用简短的8.3样式文件名而不是长文件名解决了类似的问题。我使用ShortPath
FileSystemObject
方法得到了这个简短的名字。这是一个VBScript示例......您可能希望在Java中尝试类似的东西。
Function GetShortPath(strLongPath)
Dim FSO
Dim objFolder
Dim objFile
Dim strShortPath
Set FSO = CreateObject("Scripting.FileSystemObject")
' Is it a file or a folder?
If FSO.FolderExists(strLongPath) Then
' It's a folder.
Set objFolder = FSO.GetFolder(strLongPath)
strShortPath = objFolder.ShortPath
ElseIf FSO.FileExists(strLongPath) Then
' It's a file.
Set objFile = FSO.GetFile(strLongPath)
strShortPath = objFile.ShortPath
Else
' File not found.
strShortPath = ""
End If
GetShortPath = strShortPath
End Function
例如,
Debug.Print GetShortPath("C:\öêåéèüø.çõâ")
返回C:\B373~1
,可以用来代替带有非英文字符的长文件名。示例dir /x
(显示短文件名)和记事本:
C:\sandbox>dir /x
Volume in drive C has no label.
Volume Serial Number is BC90-DF37
Directory of C:\sandbox
13/01/2011 15:12 <DIR> .
13/01/2011 15:12 <DIR> ..
13/01/2011 14:52 22 NEWTEX~1.TXT New Text Document.txt
13/01/2011 15:05 0 C7F0~1.TXT öêåéèüø.txt
13/01/2011 15:05 0 B373~1 öêåéèüø.çõâ
3 File(s) 22 bytes
2 Dir(s) 342,158,913,536 bytes free
C:\sandbox>notepad B373~1