Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

cmd

netbeans

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?

like image 515
user3066571 Avatar asked Jan 01 '26 15:01

user3066571


1 Answers

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.

like image 166
Darshan Mehta Avatar answered Jan 03 '26 05:01

Darshan Mehta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!