0

I am new to programming, java and stackoverflow. I am starting with Herbert Schidlt 9th edition and compiling first program Example. java . It gets compiled when I type path "C:\Program Files\Java\jdk1.8.0_91\bin\javac" Example.java But it do not gets compiled when I type java Example.java and gives error "could not find or load main class Example"

I tried changing Path to C:\Program Files\Java\jdk1.8.0_91\bin or C:\Program Files\Java\jre1.8.0_91\bin or C:\Program Files\Java\jdk1.8.0_91\bin\javac that I learnt from various Stackoverflow discussions but it do not works.

Also compiler check do not works with javac -version but works well with java -version

Please help. I am stuck up with this problem from last 5 days and have tried many things from various discussions.

Latecomer
  • 1
  • 1

1 Answers1

-1

To compile, use javac Example.java. java Example for running the compiled class. You are using java Example.java. You are confused when to use java and javac.

Please refer this - https://teamtreehouse.com/community/difference-between-java-and-javac

Also this - What does "Could not find or load main class" mean?

Community
  • 1
  • 1
Raghuram Vadapalli
  • 1,190
  • 2
  • 13
  • 27
  • You've noticed that OP wrote *"It gets compiled when I type path "C:\Program Files\Java\jdk1.8.0_91\bin\javac" Example.java"*, right? *"But it do not gets compiled when I type java Example.java"* Is obviously a misunderstand what "java" does. – Tom Jun 16 '16 at 16:13
  • I think the real problem is at first step when I type javac Example.java it shows 'javac' is not recognized as an internal or external command..... and the answer for all such discussions goes back to path which I mentioned earlier that I have set. but no luck till now. And I deleted the old path rather adding on it so now I don't know what old path was and have I screwed something badly. Sorry all I am new and I think a bit stupid – Latecomer Jun 16 '16 at 16:59
  • Did you try restarting your command line after changing the environment variables. – Raghuram Vadapalli Jun 16 '16 at 17:03
  • yes i even restarted my pc but no luck – Latecomer Jun 16 '16 at 17:10
  • currently it is C:\Program Files\Java\jre1.8.0_91\bin\ – Latecomer Jun 16 '16 at 17:11
  • i dont know what restarting command line means. it always starts with C:\users\samsung> on which I add cd compsci and enter to go to my folder where program is kept – Latecomer Jun 16 '16 at 17:13
  • Add "C:\Program Files\Java\jdk1.8.0_91\bin\" to your path now, open and close command line and type `javac -version` and check the output – Raghuram Vadapalli Jun 16 '16 at 17:19
  • guys I cracked it. I compiled with javaw Example.java then executed java Example and it worked. I learned somewhere in forum that javaw is used when we dont need command prompt or something like that but I used it in command prompt just to see my program running. Please help to understand why javac is not being recognized. – Latecomer Jun 16 '16 at 17:25
  • javaw is same as java except that it is windowed. Delete Example.class and run the same command again. I don't think it works. javac is for compiling and javaw is for executing. One cannot replace the other. – Raghuram Vadapalli Jun 16 '16 at 17:33