【发布时间】:2013-03-13 01:41:04
【问题描述】:
所以我有这个 .jar 文件,我试图通过 Windows 7 命令提示符运行它。我可以使用命令 java -jar myJar.jar 让它运行,它开始运行。然后我要求用户输入文件名(出于测试目的,这是 testFile1.asm),它会显示以下消息:
(文件名、目录名或卷标语法不正确)asm
在 java.io.FileInputStream.open(本机方法)
在 java.io.FileInputStream.(init)(未知来源)
在 java.io.FileInputStream.(init)(未知来源)
在 java.io.FileReader.(init)(未知来源)
在 Assembler.firstPass(Assembler.jgava:33)
在 Assembler.main(Assembler.java:29)
它在我的 Linux 终端上运行良好,但我需要让它在 Windows cmd 上运行,这样我的教授才能看到它工作正常。如果它是相关的,这是我的 java 类。
import java.io.*;
public class Assembler {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
int x;
System.out.println("Please enter a file name.");
String file ="";
for(int i = 0; ;i++){
x = System.in.read();
if (x == -1 || x == 10){
break;
}
file = file + (char)x;
}
firstPass(file);
}
static private void firstPass(String url) throws FileNotFoundException, IOException{
BufferedReader reader = new BufferedReader(new FileReader(url));
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("symbol_table.txt"), "utf-8"));
int LC = 0;
String currLine = reader.readLine();
while(currLine != null){
if(currLine.charAt(3) != ','){ //No Label present
if(currLine.contains("ORG")){ //ORG is present
LC = Integer.parseInt(currLine.substring(9,12));
LC++;
}
else if(currLine.contains("END")){
//secondPass();
break;
}
else {
LC++;
}
}
else{ //Label is present
writer.write(currLine.substring(0,3) + " " + LC +"\r\n");
LC++;
}
currLine = reader.readLine();
}
writer.close();
}
}
【问题讨论】:
-
这是一种从标准输入获取字符串的冗长方法。您是否确认您正在传递您认为的文件名?文件是否位于同一目录中?您是否考虑到 Windows 和 Linux 对 EOL 使用不同的字符? (CR 诉 CRLF)
-
我建议从问题中删除程序集标签。尽管您似乎正在编写汇编程序,但这样的问题与汇编语言编程无关。
-
for循环在您确切知道该块必须执行多少次时使用,在没有结束条件的情况下使用它们并在其中使用break是无稽之谈。请改用while。
标签: java jar command-prompt executable-jar