简体   繁体   中英

Getting FileNotFoundException in Ubuntu 18.04 with Java

Compiling of my java program seems to work okay, but when I try to run it using a file as one of the String[] args, I get a file not found exception. However, the file is present in the directory, and I've triple checked the spellings. Here's what I'm trying to do: 在此处输入图像描述

And here's the directory showing my file clearly in it在此处输入图像描述

And when I do an ls command on this directory, only my.java and.class files show up. None of the.txt files. Could this have something to do with my problem? 在此处输入图像描述

Can anyone help?

Edit: code of file I'm trying to run

import java.io.File; 
import java.io.FileNotFoundException;
import java.util.Scanner; 
public class cs3421_emul {
    public static void main(String[] args) {
        try {
            String directory = System.getProperty("user.dir");
            directory = directory.replace("\\", "\\\\");
            //File file = new File(directory + "/" + args[0]);
            File file = new File(args[0]);
            Scanner sc = new Scanner(file);
            StreamHandler stream = new StreamHandler(sc);
            while(sc.hasNext()) {
                String datatype = sc.next();
                switch(datatype) {
                    case("memory"):
                        stream.HandleMemory();
                    break;
                    case("clock"):
                        stream.HandleClock();
                    break;
                    case("cpu"):
                        stream.HandleCPU();
                    break;
                }
            }
            sc.close();
        }
        catch(FileNotFoundException e) {
            System.out.print(e);
        } 
        /*
        String size = "0x10000";
        //System.out.println("resolve substring issue " + size.substring(2));
        Memory test = new Memory(size);
        test.reset();
        String[] hexvalues = {"0x0A", "0x0B", "0x2A", "0x2B", "0x11", "0x44", "0x23", "0x93", "0x50", "0x22", "0xAE", "0xDE", "0xAD", "0xBE", "0xEF", "0xFF"};
        test.set("0x1A00", "0x10", hexvalues);
        String[] hexvalues1 = {"0xAE", "0xDE", "0xAD", "0xC0", "0xDE", "0FA", "0xCE", "0xFE", "0xED", "0xCA", "0xFE", "0xBE", "0xEF", "0x30", "0xA8", "0xEE"};
        test.set("0x1A10", "0x10", hexvalues1);
        String[] hexvalues2 = {"0x55", "0xAA", "0x10", "0x20"};
        test.set("0x1A20", "0x04", hexvalues2);
        test.dump("0x1A04", "0x20");
        //String[] testmemory = test.getMemory();
         */
    }

}

There is something very confusing going on here.

  1. Your first screenshot shows that you are running a class whose name is cs3421_emul . I presume that corresponds to the source code. It shows that you are in a directory whose name is /home/se/cs3421_emul .

    Assuming you haven't done something really weird, this directory will be the current directory when you run the command (as per this screenshot).

  2. Your second screenshot is a view of a directory described as LocalState > rootfs > home > se > cs3421_emul . That directory listing shows a file called Sample1_input.txt . It does NOT show a file called cs3421_emul.class file.

  3. Your third screenshot is the output of running ls in /home/se/cs3421_emul . It shows the cs3421_emul.class file. It does NOT show a file called Sample1_input.txt .

From this, I can only conclude that the directory in your second screenshot is a different to the one that is the current directory when you ran ls (screenshot three) and when you attempted to run your program using the java command (screenshot one).

Now I don't know what that is that you are showing us in the second screenshot. Is this some view of a Windows file system that is (somehow) being mapped to your Linux? Or is it a directory that you are copying to the Linux system?

Either way, screenshots one and three are definitive. They showing the Linux file system from the Linux perspective. And in that perspective, there is no Sample1_input.txt file in the /home/se/cs3421_emul directory. There are no *.txt files there at all.

When you run the java command like that, on the Linux system, the JVM needs to see a file with the name Sample1_input.txt in the /home/se/cs3421_emul directory.If it is not there, that would cause a FileNotFound exception with a message is identical to the one you are getting.

You need to figure out why the file Sample1_input.txt is not in the ls output; ie why the file isn't there.

BE SURE that you write the filename Exactly as what it is. Because Linux is case sensitive and for example, if you have a file with name "Sample" and you try to open "sample" file, you will get the error.

I think you have the same problem as this problem . check it out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM