3

I cant understand what the error of this code.

public void run(String url) {

        try {
            FileInputStream file;
            file = new FileInputStream(this.getClass().getResource(url));
            Player p = new Player(file);
            p.play();
        }catch(Exception e){
            System.err.print( url + e);
        }
    }

when i try to run it, it says me "no suitable constructor found for FileInputStream(URL)". Why its happening?

Tushar Monirul
  • 4,343
  • 9
  • 32
  • 47

4 Answers4

3

Use:

  • getClass().getResourceAsStream(classpathRelativeFile) for classpath resources
  • new FileInputStream(pathtoFile) for file-system resources.
Bozho
  • 554,002
  • 136
  • 1,025
  • 1,121
2

It is simpler to use getResourceAsStream

InputStream in = getClass().getResourceAsStream(url);
Player p = new Player(file);
Dedyshka
  • 435
  • 3
  • 8
  • 20
Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
2

Put the file in root of folder of your class path (folder where your .class files are generated) and then use statements below:

  InputStream inputStream = 
                  getClass().getClassLoader().getResourceAsStream(filePath);
  Player p = new Player(inputStream );

Here filePath is the relative file path w.r.t. the root folder.

Yogendra Singh
  • 32,067
  • 6
  • 59
  • 71
  • thanks a lot. its working. but you know i cant understand when, how and where we need to use it. Its very complicated for me. – Tushar Monirul Dec 15 '12 at 20:52
  • @Tushar: What is complicated? Let me know, I will try explaining to you. – Yogendra Singh Dec 15 '12 at 20:56
  • I just want to know how the line getClass().getClassLoader().getResourceAsStream(filePath); works. – Tushar Monirul Dec 15 '12 at 20:59
  • 1
    @Tushar: getClass().getClassLoader() finds the class loader of your current class where you have written this code. The classloader is aware of the location from where it loaded your class file. This is file was required to be placed in the same location. getResourceAsStream reads your file from the location and returns the input stream. Let me know, if you need more explanation. – Yogendra Singh Dec 15 '12 at 21:33
  • sure, I will be great full if you provide me more explanation. – Tushar Monirul Dec 15 '12 at 21:56
  • @Tushar: Put your new question. I will try help you understand. – Yogendra Singh Dec 15 '12 at 22:14
1

The parameter of FileInputStream constructor is File, String ... (see http://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html ), but Class.getResource return URL (see http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html), not File, or String. Try to use

public void run(String url) {

    try {
        FileInputStream file;
        file = new FileInputStream(new File(this.getClass().getResource(url).toURI()));
        Player p = new Player(file);
        p.play();
    }catch(Exception e){
        System.err.print( url + e);
    }
}
Cong Nguyen
  • 168
  • 1
  • 8
  • The method Class.getResource(String) return a URL, while the parameters of constructor of File is String, or URI... (see http://docs.oracle.com/javase/6/docs/api/java/io/File.html). The URL.toURI() method return an URI object which represent the URL. You can see the different between URI and URL in http://stackoverflow.com/questions/176264/whats-the-difference-between-a-uri-and-a-url and http://en.wikipedia.org/wiki/Uniform_resource_identifier – Cong Nguyen Dec 16 '12 at 02:50