0
votes

So I have this .jar file that I am trying to run through Windows 7 Command Prompt. I can get it running with the command java -jar myJar.jar, and it starts to run. I then ask the user to input the file name (for testing purposes this is testFile1.asm), and it shows the following message:

(The filename, directory name, or volume label syntax is incorrect)asm
      at java.io.FileInputStream.open(Native Method)
      at java.io.FileInputStream.(init)(Unknown Source)
      at java.io.FileInputStream.(init)(Unknown Source)
      at java.io.FileReader.(init)(Unknown Source)
      at Assembler.firstPass(Assembler.jgava:33)
      at Assembler.main(Assembler.java:29)

It works fine on my Linux terminal, but I need to get it working on Windows cmd so my prof can see that it works. And in case it's relevant, here's my java class.

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();
  }
}
2
That's a long-winded way of getting a string from stdin. Have you verified you're passing the filename you think you are? Is the file located in the same directory? Are you taking into account that Windows and Linux use different characters for EOL? (CR v. CRLF)Dave Newton
I suggest removing the assembly tag from the question. Although you appear to be writing an assembler, the question as such isn't related to assembly language programming.Michael
for cycles are used when you know exactly how many times the block has to be executed, it is a nonsense to use them without an end condition and using a break into them. Use a while instead.BackSlash

2 Answers

0
votes

On Windows is CR LF (ascii 13 then ascii 10). On linux and in cygwin, is just LF. So you need to check x == 13 as well.

0
votes

It's the line:

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

From InputStream API

public abstract int read()

Returns: the next byte of data, or -1 if the end of the stream is reached.

Print the value of url to make sure.

read() method return even the new line character you entered. This is handled differently in Windows and Linux. Use a BufferedReader and try readLine() method, or something like that.