comatose
comatose

Reputation: 1962

running java application from command line

I have an eclipse project which I want to run from the command line in ubuntu. After searching on the internet I am trying the following command.

java -cp . com.abc.utils.MyClassName

I issue this command from the directory that eclipse is using to store all the class files. But I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/abc/utils/MyClassName

what am I doing wrong and how to run the application from the command line ?

Upvotes: 0

Views: 379

Answers (3)

Himanshu Lakhara
Himanshu Lakhara

Reputation: 80

Your command is right. You have missed something. Please check that

1. You have compiled your code and the MyClassName.class file is created in classes/com/abc/utils/ directory.

2. You are running this command from proper directory. Your current directory should be classes directory where your com directory is located. If it's not then switch to classes directory

$cd /path/to/project/folder/classes/

Hope that helps.

Additionally if you want to learn how eclipse automatically places .class and .java files then try compiling a multi-package application by yourself.

$javac -d would be helpful.

Upvotes: 1

Jayant Varshney
Jayant Varshney

Reputation: 1825

you should set class path first

Upvotes: 0

Silmarillium
Silmarillium

Reputation: 149

You can only run .class files aka compiled java files. So you first need to compile your java file with the 'javac' command. This will generate a .class file. Then you can run the java class file with the 'java' command.

Upvotes: 0

Related Questions