0

I am facing an error when run the below java program on windows command prompt. Basically, I am a C/C++ programmer , but i need to run a java file as a part of some testing..

Following is the content of my TestClass.java, not copied full code as it looks like some kind of path or package issue.

package parse_signature;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TestClass {
    public static void main(String[] args) throws IOException {
    String path_of_file= "input/content.txt";
    BufferedReader reader = new BufferedReader(new FileReader(path_of_file));
    String line;
    while ((line = reader.readLine()) != null) {
    ....
    }
}

I am in the directory: C:/Users/Desktop/JavaPRGs/Test/, when i used the command javac TestClass.java,

TestClass.class got created and when i run java TestClass

I get following error:

Error: Could not find or load main class TestClass
Caused by: java.lang.NoClassDefFoundError: parse_signature/TestClass (wrong name: TestClass)

I tried going back to previous directory C:/Users/Desktop/JavaPRGs and ran java Test.TestClass got the same error (only this difference wrong name: Test/TestClass)

Also tried follwing, but the result is same.

C:\Program Files\Java\jdk-12.0.1\java -cp . Test.TestClass
7
  • can you put your source code online so i can replicate it? here's a tutorial: beginnersbook.com/2013/05/first-java-program Commented Jun 19, 2020 at 8:36
  • your class is in a package, are you keeping that in mind? also: why is your main method throwing an Exception? you want your application to crash? Commented Jun 19, 2020 at 8:36
  • @doppelgunner no, everything that's needed to reproduce the problem should be in the question. Commented Jun 19, 2020 at 8:37
  • 1
    Java expects the package to be part of the path to the class file. Commented Jun 19, 2020 at 8:44
  • 1
    docs.oracle.com/javase/tutorial/java/package/managingfiles.html Commented Jun 19, 2020 at 11:12

1 Answer 1

1

You need to remove the package and try to compile and run it again using javac TestClass.java and java TestClass

Sign up to request clarification or add additional context in comments.

3 Comments

sorry for late reply, the sample program (Hello World) i have already tried that is working fine, because it does not include any packages, in your second comment you mentioned to remove the package , i have removed , now this time the error is removed, but i dont get any output, i have updated the code with few more lines, plz guide me
@sravs, the code snippet you shared does not actually have any output instructions so of course it won't output anything.
@Taschi, actually that part i have not pasted , i was referring to my code, anyway the solution for the problem is solved by both doppelgunner and dave

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.