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

21
2014-04
  • 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"),
                IRI.create(fileBase));
    

    but when i run the project i got the following exception

    org.semanticweb.owlapi.io.OWLOntologyInputSourceException: java.io.FileNotFoundException: 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:

    java.io.FileNotFoundException: 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);
    writer.write("data");
    

    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");
    file.mkdirs();
    // ...
    
  • 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);
        folder.mkdir();
    
        if (!folder.exists()) {
            try {
                folder.createNewFile();
            } 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()) {
                    file.createNewFile();
                }
    
                byte[] content_in_bytes = content.getBytes();
    
                output.write(content_in_bytes);
                output.flush();
                output.close();
    
            } catch (IOException ex) {
                Logger.getLogger(JsonGeneration.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    if (output != null) {
                        output.close();
                    }
                } 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.