Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create Path object from a string

Tags:

java

path

io

nio

I'm following along the Basic I/O Tutorial on Oracle.com, but I'm having difficulty making a Path object:

Path p1 = Paths.get("/tmp/foo");

Which gives the error:

error: The method get(URI) in the type Paths is not applicable for the arguments (String).

I'm on Linux and I'm working in Eclipse Kepler. I'm trying to access a text file in the current directory. Using Scanner and File I can work with the file, but I'd also like to fiddle around with a path to the file so I can continue with the tutorial.

edit: The entirety of the program is below. The second half is me being a rookie and confirming the file exists/works. When I comment out the Path definitions, I get the output of "Test" which is in the 'save.txt' file.:

package projectSARA;
import java.util.*;
import java.io.*;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {

    public static void main(String[] args) {

        String saveFile = "save.txt";
        Path p1 = Paths.get(saveFile);
        Path p2 = Paths.get("save.txt");

        File file = new File(saveFile);
        try{
        Scanner in = new Scanner(file);
        String test = in.next();
        System.out.println(test);
        }
        catch(FileNotFoundException e){
            System.out.println("File not found");
        }
    }// end main

}
like image 268
user3345488 Avatar asked Mar 20 '23 02:03

user3345488


1 Answers

It appears to be a problem of the (default) JRE settings in Eclipse.

To solve it, in the Package Explorer, right-click the "JRE System Library" > properties.

Select "Execution environment", then select "JavaSE-1.7 (java-7-oracle)", press OK.

It happened to me when creating a new project outside the workspace.

like image 193
cybfox Avatar answered Mar 29 '23 05:03

cybfox