0

Greetings to all from a newbie,

I just completed reading Java: A Beginner's Guide Sixth Edition by Herbert Schildt, and started reading OCA Java SE 8 Programmer I Study Guide by Finegan and Liguori. Working through the very first chapter, I've already got a compiler error when trying Exercise 1-1.

Here's the code:

import java.io.*;
import java.text.*;
import java.time.*;
import java.time.format.*;
import java.util.*;
import java.util.logging.*;

public class TestClass {
  public static void main(String[] args) throws IOException {
    /* Ensure directory has been created */
    Files.createDirectories(Paths.get("logs"));
    /* Get the date to be used in the filename */
    DateTimeFormatter df
      = DateTimeFormatter.ofPattern("yyyyMMdd_hhmm");
    LocalDateTime now = LocalDateTime.now();
    String date = now.format(df);
    /* Set up the filename in the logs directory */
    String logFileName = "logs\\testlog-" + date + ".txt";
    /* Set up Logger */
    FileHandler myFileHandler = new FileHandler(logFileName);
    myFileHandler.setFormatter(new SimpleFormatter());
    Logger ocajLogger = Logger.getLogger("OCAJ Logger");
    ocajLogger.setLevel(Level.ALL);
    ocajLogger.addHandler(myFileHandler);
    /* Log Message */
    ocajLogger.info("\nThis is a logged information message. ");
    /* Close the file */
    myFileHandler.close();
  }
}

When I tried to compile this on the command line

javac TestClass.java

I get the errors

TestClass.java:11: error: cannot find symbol

Files.createDirectories(Paths.get("logs"));
                        ^

symbol: variable Paths

location: class TestClass

TestClass.java:11: error: cannot find symbol

Files.createDirectories(Paths.get("logs"));
^

symbol: variable Files

location: class TestClass

2 errors

This is my first time attempting to use the Files and Paths classes, which are supposed to be part of java.lang. Could anyone explain why I get these errors and what I might do to resolve this?

Thanks, Frank

  • Where is your evidence for _which are supposed to be part of java.lang_? – Sotirios Delimanolis Feb 15 '18 at 18:31
  • Thank you for replying to my question. The reason I though that the Files class and the Paths class are part of java.lang is because that's what I read in Java™ Platform, Standard Edition 8 API Specification at https://docs.oracle.com/javase/8/docs/api/. – Frank Moor Feb 15 '18 at 18:41
  • I clicked on both of those classes and they definitely weren't in the `java.lang` package, so I don't know where you're looking. – Sotirios Delimanolis Feb 15 '18 at 18:43
  • Thanks. You are right. I misunderstood the API document, and it turns out that that they are actually in java.nio.file. When I updated the code to import them, it worked. – Frank Moor Feb 15 '18 at 18:48

0 Answers0