Question

I know this question has already been answered many times, but unfortunately I couldn't find the right answer to my questions.

below is my package structure and inside my package I have SimpleTest.java

d:\junit\src\junitfaq\SimpleTest.java

inside d:\junit\src> i tried to compile SimpleTest.java and it successfully compiled using the command below.

d:\junit\src>javac junitfaq/SimpleTest.java

but when i try to run the program using command line below

d:\junit\src>java junitfaq.SimpleTest

this error occured. Error: Could not find or load main class junitfaq.SimpleTest

I tried running it by accessing junitfaq package by using this command

d:\junit\src\junitfaq>java -cp . SimpleTest

the program run perfectly. A little help would be much appreciated.

Was it helpful?

Solution

Have you declared your SimpleTest class to be a member of the junitfaq package? If you have, you should be able to run it from the src directory like java junitfaq.SimpleTest but you should get an error like this if you try to run it from within the junitfaq directory: Exception in thread "main" java.lang.NoClassDefFoundError: SimpleTest (wrong name: junitfaq/SimpleTest)

Make sure your SimpleTest class starts with package junitfaq;

Edit: Here's a working example incorporating the comments below.

login@domain:~/temp> mkdir src
login@domain:~/temp> cd src
login@domain:~/temp/src> mkdir junitfaq
login@domain:~/temp/src> nano junitfaq/SimpleTest.java

The contents of SimpleTest.java are as follows when I exit nano:

package junitfaq;

public class SimpleTest {
        public static void main(String[] args) {
                System.out.println("Test");
        }
}

login@domain:~/temp/src> javac junitfaq/SimpleTest.java
login@domain:~/temp/src> java junitfaq.SimpleTest
Test

OTHER TIPS

It sounds like you have a classpath problem; you should double-check the location of your class file, the directory/package structure, the location from which you're trying to run the java command, and any classpath specified during execution.

For example, the following works for me:

$ mkdir junitfaq
$ cat >junitfaq/SimpleTest.java
package junitfaq;
public class SimpleTest
{
    public static void main(String[] args)
    {
        System.out.println("Hello, world!");
    }
}
^D
$ javac junitfaq/SimpleTest.java
$ java junitfaq.SimpleTest
Hello, world!
$ java -cp . junitfaq.SimpleTest
Hello, world!
$ 

Not to belabor the obvious, but I noticed a spelling typo in one of your comments - you should also double-check that you're running the command as intended.

trialYou could try putting your java and class files together with your libaray folder (if any) in a new file called "trial" in your C:\ directory then compile existing java file using the following->

C:\trial> javac -cp .;library folder* SimpleTest.java

(then)

C:\trial> java -cp .;library folder* SimpleTest

Let me know how you get on!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top