java - Servlet threw exception "The system cannot find the path specified"

  • Sami

    in my servlet im writing the following statement to load the ontology file

    File fileBase = new File("src/java/ontology/ontology.owl");
    SimpleIRIMapper iriMapper =  new SimpleIRIMapper(IRI.create("src/java/ontology/AOA.owl"),

    but when i run the project i got the following exception src\java\ontology\ontology.owl (The system cannot find the path specified)

    how can i overcome this problem ?

  • Answers
  • javapirate

    provide the absolute path / place the file in class path as File fileBase = new File("ontology.owl");

  • Related Question

    java - FileNotFoundException (The system cannot find the path specified)
  • Michael

    I get this exception: C:\...\filename.xml (The system cannot find the path specified)

    using this code:

    FileWriter fileWriter = new FileWriter(new File(path + date + time "filename.xml"));
    BufferedWriter writer = new BufferedWriter(fileWriter);

    Path exists but directories for 'date' and 'time' need to be created. Application has full permissions on the directory.

    Any ideas?

  • Related Answers
  • BalusC

    The problem is because I'm creating a subdirectory in which to write the files. So I currently have C:\example\ and want to write my files in C:\example\<date>\<time>\<files>

    You need to call File#mkdirs() before writing.

    File file = new File("C:/example/newdir/newdir/filename.ext");
    // ...
  • Frankie

    Do assume that the computer is right and you are wrong.

    And, in that scenario, the directory to which you want to write does not exit (or does not have permissions to do so).

    1. check the current working dir System.getProperty("user.dir")
    2. debug from there
  • jzd

    Code works for me. (Need to add a writer.close() for text to show up in the file.)

  • user3440120

    You also need to convert newly created file and folder paths to string.

    File folder = new File("src\\main\\json\\", idNumber);
        if (!folder.exists()) {
            try {
            } catch (IOException ex) {
                Logger.getLogger(JsonGeneration.class.getName()).log(Level.SEVERE, null, ex);
        FileOutputStream output = null;
            File file;
            String content = data.toString();
            try {
                String folder_location = folder.toString() + "\\";
                String filename = "CurrentInfo";
                file = new File(folder_location + filename.toString() + ".json");
                output = new FileOutputStream(file);
                if (!file.exists()) {
                byte[] content_in_bytes = content.getBytes();
            } catch (IOException ex) {
                Logger.getLogger(JsonGeneration.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    if (output != null) {
                } catch (IOException e) {
                    Logger.getLogger(JsonGeneration.class.getName()).log(Level.SEVERE, null, e);
  • sproketboy

    If the file does not exist you need to create it. Google it.