SOLVED :)
Thanks for all the help, I was simply missing a semi colon to end my initial statement.
Go figure.
Printable View
SOLVED :)
Thanks for all the help, I was simply missing a semi colon to end my initial statement.
Go figure.
Could you post the error message?
The message (and the line number of your code that it refers to) is usually helpful in identifying what went wrong.
[Edit] Also could you clarify *when* you get the error. Your original post says that it occurs when you try and run the program. But, as posted, the code won't compile.
i copied and pasted your code into my IDE. i changed a few things around and it worked in mine.
Code:import java.io.File;
public class Main {
public static void main(String[] args) {
File listroot = new File("c:/");
File[] files = listroot.listFiles();
System.out.println("Print root files from c:");
for (File file : files) {
if (file.isDirectory())
continue;
System.out.println(file.getPath());
}
}
}
Here is the output I get when I try to run my code:
--------------------Configuration: ListFiles - JDK version 1.6.0_20 <Default> - <Default>--------------------
java.lang.NoClassDefFoundError: ListFiles
Caused by: java.lang.ClassNotFoundException: ListFiles
at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
Could not find the main class: ListFiles. Program will exit.
Exception in thread "main"
Process completed.
That error occurs because the code has not been compiled. (Hence the NoClassDefFoundError.)
In fact alacn's post corrected the line that was causing the code not to compile: put "import" at the very start before "java.io.File". Ie it should look like:
This line is telling the compiler about the identity of the class you are calling File.Code:import java.io.File;