user3066571 user3066571 - 1 month ago 5
Java Question

Execute Java Application from Command Line without package or class specification?

I'm in class, and no, before I begin, I don't need anyone to do my homework. The program is written, but the instructor said he grades using a scripted test bed, and the program (a simple Palindrome test, I'm a noob) must be executable as per his specifications or his test bed won't be able to run it, and if it doesn't run it, he isn't gonna try it manually and I fail the assignment (charmer I know.) Basically, his requirements are this:

There cannot be a package declaration in the application.

It must be executable from command line with simply this:

java Palindrome

that's it. When I navigate to the directory the file lives in and try that, it gives me
could not find or load main class Palindrome
. Looking that error up, there's lots of solutions that include leading - paramters or a fully qualified class name. I can't use either of those, because if
java Palindrome
doesn't launch it, I get a 0.

Can anyone tell me how to get it to run without any parameters or fully qualified class names? Just
java Palindrome


When you execute java <filename> it tries to find the file in the current directory. If it's not found, it returns an error saying could not find or load main class.

So, in your case, you are either executing java command from different directory or you haven't compiles the class. For the former, you can specify the class file path with -cp argument to command (e.g. java -cp FULLPATH CopyFile), for the latter case, you need to compile the class with javac first and then, execute it.