0

Error:

Could not find or load main class CLASSNAME

Command:

java -cp "Path to MAIN class" "MAINCLASSNAME"

When I copy and paste the path, it is correct and when I go to the path and run the command it works fine.

So can anyone help me and tell me what is going on here and how to fix it?

java -cp /usr/home/dir1/2014Dir/Team1/Problem1/05:09:55/ Prob1
Error: Could not find or load main class Prob1
$

ls -l /usr/home/PCWSA/2014ConDir/Team1-14/Problem1/05:09:55/03-21-14_07.05.17:
total 4
-rw-r--r--  1 PCWSA  PCWSA  1359 Mar 21 07:05 Prob1.class
user2597012
  • 581
  • 4
  • 9
  • 28

3 Answers3

0

Example to run the program ..

To compile the program go to src directory in command line and type the following

C:/blah/src > javac com\sample\MAINCLASSNAME.java

To run the program use the following command

C:/blah/src > java -cp . com.sample.MAINCLASSNAME

Use the command javac -d to specify the place where u want to generate your class files .. Then run the java command from that directory.

Raveesh Sharma
  • 1,486
  • 5
  • 21
  • 38
  • But I am using a program to go and compile and run java applications, so that program takes the path as an argument. – user2597012 Mar 21 '14 at 11:55
  • Am i getting this right ? . You are using a Runtime.exec or some process builder to compile and run other java programs ? – Raveesh Sharma Mar 21 '14 at 12:00
  • Exactly! that is what i am doing – user2597012 Mar 21 '14 at 12:03
  • +1 Also , perhaps you should read up on http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html .. It can give u some help in case u are making some mistakes while invoking.. Cos as OscarRyz said .. we cant see your computer – Raveesh Sharma Mar 21 '14 at 12:12
  • Well I'm sorry about the rage comment, but, it's very hard to help you if you don't tell us what are you doing. So, based on your last edit I added an answer, it looks like you're using different directories. – OscarRyz Mar 21 '14 at 12:14
  • Thanks for your help. the problem still exist though! Anyway I will spend more time working on it to see why it exist. – user2597012 Mar 21 '14 at 12:19
  • ummm... did you read my answer? You have two very different directories there. Isn't that the problem? – OscarRyz Mar 21 '14 at 12:21
  • Yeah I fixed it and the same thing happened. The issue was that the path contain "." in it and I think was causing the issue. I am making sure now! – user2597012 Mar 21 '14 at 12:23
  • @user2597012 Please use the following command javac -d "/usr/home/dir1/2014Dir/Team1/Problem1/05:09:55/" MainClassName.java and then traverse to /usr/home/dir1/2014Dir/Team1/Problem1/05:09:55/ . Now run java -cp . .MainclassName – Raveesh Sharma Mar 21 '14 at 12:25
  • Or probably it could be the path contains `:` which is a directory separator in unix – OscarRyz Mar 21 '14 at 12:25
0

Based on your last edit, it looks like you're trying find your .class file in the wrong directory

You're trying to execute it on:

/usr/home/dir1/2014Dir/Team1/Problem1/05:09:55/

But the file is on:

/usr/home/PCWSA/2014ConDir/Team1-14/Problem1/05:09:55/03-21-14_07.05.17:

So just use that directory instead:

java -cp "/usr/home/PCWSA/2014ConDir/Team1-14/Problem1/05:09:55/03-21-14_07.05.17:" Prob
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
0

So I figured what the problem was! My path to the MAINCLASS contained dots '.' and that was causing the issue, when I renamed the folders it worked fine. I hope this is going to be helpful.

user2597012
  • 581
  • 4
  • 9
  • 28
  • Just to avoid you having the same problem later. Is is not the dots, but the colon. This works properly `java -cp /some.path/ Blah` but this doesn't `java -cp /some:path Blah` You have to either use quotes or escape it: `java "/some/path" Blah" or `java /some\:path Blah` – OscarRyz Mar 21 '14 at 12:30