Reputation: 43
I'm working on a text editor project right now for my programing class and I'm getting an error I've never seen before when I try to run it. It's a fairly long explanation, but basically, i'm Using an editor class that uses several other classes to create a linked list, store a text file in it, and then allow modification of the file. I'm supposed to run it in a linux environment, and the file in question is supposed to be entered as a 'command-line' argument. However, every time I try running it, I get the following error
Exception in thread "main" java.lang.NoClassDefFoundError: myEditor
Caused by: java.lang.ClassNotFoundException: myEditor
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: myEditor. Program will exit.
As for the program itself:
import java.util.Scanner;
import java.util.Iterator;
import java.io.*;
public class myEditor {
public static void saveToFile(String text, String filename) throws IOException{
PrintWriter out = new PrintWriter(new File(filename));
out.println(text);
out.close();
}
public static void main(String args[]) {
boolean quit = false;
try {
if(args.length!=1) {
throw new IllegalArgumentException();
}
String filename = args[0];
Scanner input = new Scanner(new File(filename));
//Add exception
UnorderedList<String> list = new UnorderedList<String>();
while(input.hasNextLine()) {
if(list.head==null) {
list.addToFront(input.nextLine());
}
list.addToRear(input.nextLine());
}
System.out.println(">");
do {
Scanner command = new Scanner(System.in);
String comm = command.next();
String[] comm1 = comm.split(" ");
if(comm1[0].equalsIgnoreCase("I")&&comm1[1].equals("")) {
System.out.println("Type a line of text >");
comm = command.next();
list.addToRear(comm);
}
else if(comm1[0].equalsIgnoreCase("I")&&!comm1[1].equals("")) {
int linNum = Integer.parseInt(comm1[1]);
Iterator<String> itr = list.iterator();
String current = "";
for(int count=0;count<linNum;count++) {
current = itr.next();
}
list.addAfter(comm, current);
}
else if(comm1[0].equalsIgnoreCase("D")&&!comm1[1].equals("")) {
int linNum = Integer.parseInt(comm1[1]);
if(linNum<=list.count&&linNum>0) {
Iterator<String> itr = list.iterator();
String current = "";
for(int count=0;count<linNum;count++) {
current = itr.next();
}
list.remove(current);
}
}
else if(comm1[0].equalsIgnoreCase("L")) {
list.toString();
}
else if(comm1[0].equalsIgnoreCase("E")&&!comm1[1].equals("")) {
saveToFile(list.toString(), filename);
quit = true;
break;
}
}
while(!quit);
}
catch(IllegalArgumentException e) {
System.err.print(e.getMessage());
}
catch(FileNotFoundException e) {
System.err.print(e.getMessage());
}
catch(IOException e) {
System.err.print(e.getMessage());
}
}
}
Obviously, there's a load of other classes I used with this one, but it seems to me the error doesn't lie in them. Does anyone have any experience with this kind of error?
EDIT: I almost forgot to mention, by command line argument, I meant that the file this is supposed to be worked with should already be in the linux directory it was placed it. It should apparently take up args[0]
Upvotes: 1
Views: 2857
Reputation: 4199
java.lang.classNotFoundException comes in following cases:
1) When we try to load a class by using Class.forName() method and .class file or binary of class is not available in classpath.
2) When Classloader try to load a class by using findSystemClass () method.
3) While using loadClass() method of class ClassLoader in Java.
So it is clear that jvm not able to find your class. Now question is who will tell the jvm that where to look for a class? answer is -- CLASSPATH environment variable.
because Classpath is a parameter—set either on the command-line, or through an environment variable—that tells the Java Virtual Machine or the Java compiler where to look for user-defined classes and packages.
So you can set the CLASSPATH at run time "java $CLASSPATH:. myEditor"
or at environment as always to look for your current directory and it will fix the problem.
On linux variant you can set this at .bashrc file ( if using bash) , can use set command, can set at .bash_profile. On Windows you will find the same at "System-->Properties"
Upvotes: 2
Reputation: 397
Make sure you have compiled your class by javac
Please execute the command with classpath such as
java $CLASSPATH:. myEditor
Upvotes: 0
Reputation: 73
The error is saying that it can't find the class myEditor at runtime however was present at compile time. In my tests, when I ran the class and I was missing something it would give me that exact error. Are all your classes in the same place (Or properly organized)? Try looking over all the .class files that you need and make sure they are all there. If you provide some more details of how you're compiling and running this I can edit this answer to give you a better one!
Upvotes: 0