-1

I am getting below error when I tried to run the included program -

Could not find or load main class ConsoleDemo Caused by: java.lang.NoClassDefFoundError: inputOutput/ConsoleDemo (wrong name: ConsoleDemo)

import java.io.Console;

public class ConsoleDemo {
    public static void main(String[] args) {
        Console cn = System.console();

        System.out.print("Enter your name: ");
        String name = System.console().readLine(); //cn.readLine() will also work

        System.out.print("Enter your password: ");
        char[] pass = cn.readPassword();

        System.out.println("\n----Details---- \nName: " + name);
        System.out.println("Password: " + pass.toString());
    }
}
  • 1
    can you include how you are setting classpath & executing below program? As I don't think you need to set classpath to execute this program. – Nitishkumar Singh Jun 08 '18 at 04:39
  • @NitishkumarSingh I'm running - **java {className} -cp .** and have also tried setting the class with fully classified path in environment variables as well as via Command line - **set CLASSPATH = "full path of java file containing concerned class"** – Ankit Gupta Jun 08 '18 at 04:52
  • No need, just execute `$javac ConsoleDemo.java` and `java ConsoleDemo` thats all – dkb Jun 08 '18 at 05:50
  • @dkb I've already tried it The code compiles after running **javac ConsoleDemo.java** But gives the mentioned error on running **java ConsoleDemo** – Ankit Gupta Jun 08 '18 at 06:23
  • I got this output on executing : Enter your name: D `Enter your password: ----Details---- Name: D Password: [C@42a57993`, can you check name do not have any special hidden characters in ConsoleDemo.java name of file, try renaming file, compile again and run. – dkb Jun 08 '18 at 06:29
  • @dkb The name is correct Can you please share your classpath entries, if possible? – Ankit Gupta Jun 08 '18 at 06:33
  • can you check is $JAVA_HOME is set for your machine? – dkb Jun 08 '18 at 06:38
  • @dkb Yes. It is set to _C:\Program Files\Java\jdk-9.0.4_ – Ankit Gupta Jun 08 '18 at 06:40
  • I was using 1.8 but tried with 9.0.4 too, it worked for java 9 too., if it is classpath issue then it would not have find dependent class and throw that error but here exception clearly stats that User class is not found, so I presume it is related to ConsoleDemo.java name, can you try this, open file in editor and change encoding to linux from windows, then do javac and java. – dkb Jun 08 '18 at 06:44
  • I mean EOL conversion: ref: https://stackoverflow.com/q/16239551/2987755 – dkb Jun 08 '18 at 06:49
  • There might be a problem with the path. The message contains _inputOutput_ /ConsoleDemo. Where are your files and from where are you trying to call the class? – M.F Jun 08 '18 at 06:52

2 Answers2

0

Ok, So after looking into your question. Let's assume your java file is present within Test folder, as shown in below image:

Now, if we need to just compile .java file from command line using command javac ConsoleDemo.java

Compile Java File

After compiling, we will have one more file i.e class file for java program.

Class File

You can run this program using command java ConsoleDemo, which will execute your java code.

Nitishkumar Singh
  • 1,714
  • 1
  • 13
  • 33
  • I've already tried it The code compiles after running **javac ConsoleDemo.java** But gives the mentioned error on running **java ConsoleDemo** – Ankit Gupta Jun 08 '18 at 06:12
  • then I think there is some java installation issue with your system, as there is no additional complexity because of which your system should not be able to find the class. – Nitishkumar Singh Jun 08 '18 at 06:31
  • Can you please share your classpath entries, if possible? – Ankit Gupta Jun 08 '18 at 06:35
  • I don't have anything within my classpath. You need classpath variable only if you want to use any jar/class file which is not present within your project and is only present within the system. – Nitishkumar Singh Jun 08 '18 at 06:38
0

Thanks everyone for the inputs. After checking I found out that not only this but every java code file was giving the same error.

Everything was running fine in Eclipse but I was unable to run it from cmd. To rectify it, I just compiled the code in current directory and then ran the .class file using java -cp . {fully qualified name} (from one up directory) or java -cp .. {fully qualified name} (from current directory) In my case the fully qualified name was inputOutput.ConsoleDemo.

Once again, thanks everyone for your time and inputs