【问题标题】:java - Can't open .asm file from Command Prompt?java - 无法从命令提示符打开 .asm 文件?
【发布时间】: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


【解决方案1】:

在 Windows 上是 CR LF(ascii 13 然后 ascii 10)。在 linux 和 cygwin 中,只是 LF。所以你还需要检查 x == 13 。

【讨论】:

    【解决方案2】:

    就是这样:

    if (x == -1 || x == 10){
    

    InputStream API

    public abstract int read()

    返回: 数据的下一个字节,如果到达流的末尾,则为 -1。

    打印url的值以确认。

    read() 方法甚至返回您输入的换行符。这在 Windows 和 Linux 中的处理方式不同。使用BufferedReader 并尝试readLine() 方法,或类似的方法。

    【讨论】:

      猜你喜欢
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多