Home > database >  Java Cannot Load Main Class Without Adding .java
Java Cannot Load Main Class Without Adding .java

Time:10-25

I'm trying to run a Java class file which I've compiled, but it keeps telling me that it cannot find or load the main class.

I have a directory which looks like this: Directory

My goal is to run the RandomWord.java file, which takes in a file input and relies on the algs4.jar file found in the .lift sub-directory. From what I've seen, to compile, I need to run the following (assuming I'm cd'ed into the hello directory):
$ javac -cp .lift/algs4.jar RandomWord.java
This should create my RandomWord.class file. Now, to run it with a txt file, for example with coin.txt, I believe I should be running the following:
$ java -cp .lift/algs4.jar RandomWord < coin.txt
However, this results in an error stating: Error: Could not find or load main class RandomWord.

I've done a ton of experimenting with this but I have no idea what's going on, but I've found that running the following works:
$ java -cp .lift/algs4.jar RandomWord.java < coin.txt
However, I believe when a .class file is created, I shouldn't be writing .java in my command line, so I'm really confused.

CodePudding user response:

Windows:

$ java -cp ".lift/algs4.jar;." RandomWord < coin.txt

Linux/Mac

$ java -cp ".lift/algs4.jar:." RandomWord < coin.txt

Current dir must be added to classpath to allow java to find classes there.

  • Related