Error: Could not find or load main class- Novice

2019-03-01 07:35发布

问题:

Hi I am a novice in JAVA. I have been getting this file not found exception inspite of the file existing in the very location I have specified in the path which is

Initially I had the issue of file not found. However, after performing a clean and re-run, now I am having an issue which says

Error: Could not find or load main class main.main

import Message.*;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
public class main{  


public static void main(String[] args) {

        Message msg=new Message("bob","alice","request","Data@@@@");
        MPasser passerObj=new MPasser("C:\\Workspace\\config.txt","process1");

    }
}

Also in the MPasser Constructor the following piece of relevant code is there

public class MPasser(String file_name,String someVariable){
    InputStream input;
        try {
            input =new RandomAccessFile(file_name,"r");
        } catch (FileNotFoundException e) {
            e.printStackTrace();        
            }
    Yaml yaml = new Yaml();
    Map<String, String> Object = (Map<String, String>) yaml.load(input);
}

Sorry I have made edits from initial query so that it is more clear

回答1:

On this line:

input = RandomAccessFile("C:\Workspace\conf.txt",'r');

You need to escape the \'s

input = RandomAccessFile("C:\\Workspace\\conf.txt",'r');


回答2:

 "C:\Workspace\conf.txt"

Those are escape sequences. You probably meant:

 "C:\\Workspace\\conf.txt"

You also appear to call it config.txt in one snippet and conf.txt in the other?



回答3:

Make sure the java process has permissions to read the file.



回答4:

You have to escape the backslash.

input = RandomAccessFile("C:\\Workspace\\conf.txt",'r');

and also

 input = new RandomAccessFile("C:\\Workspace\\conf.txt",'r');

and why you have two different filename conf.txt and config.txt.