0

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

}

Additional: I've asked this question before, and was asked for edits. I added in the edits, waited a few days, but no more responses. Am I supposed to refresh it somehow, or contact the authors of the comments? I'm very new to Stack Exchange, but appreciate everyone's efforts immensely.

3
  • Is this a compile time error or run time error? This works find for me using Netbeans, Java 7 and Mac OS Commented Mar 30, 2014 at 4:17
  • Also works for me in Eclipse Kepler 4.3.2 with Java 7 and Mac OS Commented Mar 30, 2014 at 8:17
  • Paths.get(...) calls Path.of(...) so I'd call the latter. See stackoverflow.com/questions/58631724/paths-get-vs-path-of Commented Jul 9, 2020 at 14:20

5 Answers 5

0

Paths.get("/some/path") is a valid method call. If the code is as you posted it, this may be a bug with Eclipse intentions. Your error The method get(URI) in the type Paths... is not a javac error, it's an Eclipse error.

Your class is fairly simple, so try compiling it with javac or using some other IDE besides Eclipse and see if you get a compilation error.

Sign up to request clarification or add additional context in comments.

1 Comment

It is a compilation problem, since it gets reported when running a .jar outside of Eclipse: java -jar mine.jar Exception in thread "main" java.lang.Error: Unresolved compilation problems: The method get(URI) in the type Paths is not applicable for the arguments (String)
0

In your line:

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

You are using a string while the get method is expecting an URI.

Just define your path as an URI first.

URI uri = null;
try {
    uri = new URI("file:///myFile.txt");  
             }
Path file=Paths.get(uri);

Comments

0

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

where Paths.get accepts only URI arguments so we need to provide like

Path dir = Paths.get(new URI("/tmp/foo"));

I guess, it will solve your issue.

1 Comment

There are actually two methods and the other one accepts a string.
0

Your code should work, but here's a simple workaround:

Paths.get("save.txt", "");

This will force the system to use the string-based get method instead of the URI.

Comments

0

Changing the JRE to java 7 will solve this. On Eclipse go to project properties -> build path -> libraries -> select the JRE system library -> click edit -> change execution environment to jdk 7. Now you can pass String into Paths.get().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.