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