Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Compiling my basic .java works fine, running the compiled .class gives the error  RSS feed

 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you help me understand why compiling works fine but the JRE does not.
When running the same empty class below in my IDE its runs fine, just not manually on the command line.


CMD started as administrator

C:\Users\Media Markt Apeldoor>echo %PATH%
C:\WINDOWS\system32;C:\Program Files (x86)\Nmap;C:\HashiCorp\Vagrant\bin;C:\Program Files\Git\cmd;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\Java\jdk1.8.0_91\bin;C:\Program Files (x86)\Nmap;C:\Program Files\Docker Toolbox

D:\13 JAVA\Workspace_Eclipse\testing\src\testing>dir
Volume in drive D is Disk-1
Volume Serial Number is 509C-3F5F

Directory of D:\13 JAVA\Workspace_Eclipse\testing\src\testing

09/09/2016  12:53 PM    <DIR>          .
09/09/2016  12:53 PM    <DIR>          ..
09/09/2016  12:21 PM                90 Zoo.java
               1 File(s)             90 bytes
               2 Dir(s)  56,879,665,152 bytes free

D:\13 JAVA\Workspace_Eclipse\testing\src\testing>javac Zoo.java

D:\13 JAVA\Workspace_Eclipse\testing\src\testing>java Zoo
Error: Could not find or load main class Zoo

D:\13 JAVA\Workspace_Eclipse\testing\src\testing>dir
Volume in drive D is Disk-1
Volume Serial Number is 509C-3F5F

Directory of D:\13 JAVA\Workspace_Eclipse\testing\src\testing

09/09/2016  12:53 PM    <DIR>          .
09/09/2016  12:53 PM    <DIR>          ..
09/09/2016  12:53 PM               259 Zoo.class
09/09/2016  12:21 PM                90 Zoo.java
               2 File(s)            349 bytes
               2 Dir(s)  56,879,665,152 bytes free

D:\13 JAVA\Workspace_Eclipse\testing\src\testing>java Zoo
Error: Could not find or load main class Zoo

D:\13 JAVA\Workspace_Eclipse\testing\src\testing>
 
Dave Tolls
Rancher
Posts: 2911
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a CLASSPATH defined in your environment variables?
If so I would recommend removing it.
 
Liutauras Vilda
Marshal
Posts: 4634
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have package statement above the code lines you posted?
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

No package statement in the class.

What environment variable would you delete/change?
 
Dave Tolls
Rancher
Posts: 2911
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't need either of them.

Classpaths should be defined around the project you are working on.
If you're compiling from the command line (as you are) then I'd use the -cp switch to declare it if needed, but in this case it isn't.

Do you know why either of those were added?
 
Liutauras Vilda
Marshal
Posts: 4634
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arend van der Kolk wrote:Directory of D:\13 JAVA\Workspace_Eclipse\testing\src\testing
Somehow looking to your directory tree I assumed you have package statement.
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
trial and error on my own; thats how i added them.
ok, i removed them. Now i have to look up the "set" command i suppose?

 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arend van der Kolk wrote:trial and error on my own; thats how i added them.
ok, i removed them. Now i have to look up the "set" command i suppose?



Compiling still works.

D:\13 JAVA\Tutorial\Java Oracle SE8 I exam>javac Zoo.java

D:\13 JAVA\Tutorial\Java Oracle SE8 I exam>
D:\13 JAVA\Tutorial\Java Oracle SE8 I exam>java Zoo
Error: Could not find or load main class Zoo

D:\13 JAVA\Tutorial\Java Oracle SE8 I exam>
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the qoute button wasn't so clever
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is confusing for a newby like me because JRE with JVM is there

D:\13 JAVA\Tutorial\Java Oracle SE8 I exam>java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
 
Dave Tolls
Rancher
Posts: 2911
35
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still have a CLASSPATH there in that screenshot.
It has Tomcat in it.

How java work with classpaths is (ignoring executable jar files):

1.  Is there a -cp switch?  Use that.
2.  Is there a CLASSPATH?  Use that.
3.  Else use the current directory.

At the moment when you run java it is looking for your class in the Tomcat/lib directory.
There really should be no reason to have a CLASSPATH defined globally like that, but you might have an odd set up.

You could go for option 1, which would be:
java -cp . <your class here>
but I really would remove the CLASSPATH if you can.
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i deleted that Tomcat path too.

Now i get

D:\13 JAVA\Tutorial\Java Oracle SE8 I exam>javac Zoo.java
'javac' is not recognized as an internal or external command,
operable program or batch file.

D:\13 JAVA\Tutorial\Java Oracle SE8 I exam>java -version
'java' is not recognized as an internal or external command,
operable program or batch file.

 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try this based on https://docs.oracle.com/javase/tutorial/essential/environment/paths.html

D:\13 JAVA\Tutorial\Java Oracle SE8 I exam>javac C:\Program Files\Java\jdk1.8.0_91\bin\javac Zoo.java
'javac' is not recognized as an internal or external command,
operable program or batch file.
 
Dave Tolls
Rancher
Posts: 2911
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arend van der Kolk wrote:i deleted that Tomcat path too.



Can you show your environment variables again?

I suspect you deleted the PATH one and not the CLASSPATH one.
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, one moment.

by the way: Placing my .java in the JDK bin dir works but that doesn't answer my question yet.

C:\Program Files\Java\jdk1.8.0_91\bin>dir
Volume in drive C is OS
Volume Serial Number is 82A5-4D9B

Directory of C:\Program Files\Java\jdk1.8.0_91\bin

09/09/2016  03:10 PM    <DIR>          .
09/09/2016  03:10 PM    <DIR>          ..
04/27/2016  09:09 AM            15,904 appletviewer.exe
<snippet>
04/27/2016  09:09 AM            16,416 xjc.exe
09/09/2016  02:46 PM                69 Zoo.java
              55 File(s)      3,432,374 bytes
               2 Dir(s)  27,606,626,304 bytes free

C:\Program Files\Java\jdk1.8.0_91\bin>
C:\Program Files\Java\jdk1.8.0_91\bin>
C:\Program Files\Java\jdk1.8.0_91\bin>
C:\Program Files\Java\jdk1.8.0_91\bin>javac Zoo.java

C:\Program Files\Java\jdk1.8.0_91\bin>
C:\Program Files\Java\jdk1.8.0_91\bin>java Zoo

C:\Program Files\Java\jdk1.8.0_91\bin>
09/09/2016  03:10 PM               251 Zoo.class <--
09/09/2016  02:46 PM                69 Zoo.java
              56 File(s)      3,432,625 bytes
               2 Dir(s)  27,605,581,824 bytes free
 
Dave Tolls
Rancher
Posts: 2911
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I meant the screenshot of your environment variables, as you did earlier.
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, that was an old one. I di delete Tomcat path

 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I managed, ;-)

D:\13 JAVA\Tutorial\Java Oracle SE8 I exam>set path=%path%;C:\Program Files\Java\jdk1.8.0_91\bin

D:\13 JAVA\Tutorial\Java Oracle SE8 I exam>dir
Volume in drive D is Disk-1
Volume Serial Number is 509C-3F5F

Directory of D:\13 JAVA\Tutorial\Java Oracle SE8 I exam

09/09/2016  02:46 PM    <DIR>          .
09/09/2016  02:46 PM    <DIR>          ..
09/05/2016  10:09 PM         3,128,686 copy_for_the_answers.pdf
09/05/2016  08:47 PM         2,807,159 etsviewer.jar
09/05/2016  08:53 PM    <DIR>          ETSWF_com_enthuware_ets_oca-jp-i_v8
09/05/2016  08:45 PM           454,642 japv8.ets
09/05/2016  08:55 PM               819 license.zip
09/09/2016  02:46 PM                69 Zoo.java
09/05/2016  08:12 PM         3,128,706 [JAVA Oracle Certified Associate Java SE 8 Programmer I Study Guide Exam 1Z0-808.pdf
               6 File(s)      9,520,081 bytes
               3 Dir(s)  56,879,472,640 bytes free

D:\13 JAVA\Tutorial\Java Oracle SE8 I exam>javac Zoo.java

D:\13 JAVA\Tutorial\Java Oracle SE8 I exam>
D:\13 JAVA\Tutorial\Java Oracle SE8 I exam>java Zoo

D:\13 JAVA\Tutorial\Java Oracle SE8 I exam>
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But maybe that is not how it should be done when you mentioned that it should be set per applicatiuon.
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave,

Thank you very much for your explanations.

cheers,

Arend
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a big difference between the PATH environment variable and the CLASSPATH environment variable.  CLASSPATH should normally be empty unless you know of a really good reason it should not be.  PATH need to have an entry with the file path to the JDK bin directory.  You did that when you executed "set path=%path%;C:\Program Files\Java\jdk1.8.0_91\bin ".  Now you should be able to execute:

javac -version

and get a version number back.  It looks like you have done this.  My only concern is if the change to the PATH variable is permanent.  Try closing your command window, opening it again, and executing the above command.  If it works, you're good.  If not, click here and look for the "Setting your JAVA_HOME and PATH variables" section.
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When setting the PATH variable with "set path=%path%... etc" it enters it in the windows OS but it does not show up in the GUI of the Environment Variables of the System Properties.

What is the difference between the CMD command line setting or setting it via the GUI in System Properties.

D:\13 JAVA\Tutorial\Java Oracle SE8 I exam>javac -version
javac 1.8.0_91

D:\13 JAVA\Tutorial\Java Oracle SE8 I exam>echo %path%
C:\WINDOWS\system32;C:\Program Files (x86)\Nmap;C:\HashiCorp\Vagrant\bin;C:\Program Files\Git\cmd;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\Java\jdk1.8.0_91\bin
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The PATH set via a GUI or similar remains the same until you change it. The PATH set via the command line with the SET PATH=xyz;pqr;abc;%PATH% command is only valid for the lifetime of that particular command window. At least I think it is.

By the way: put the new Java® installation folder first in the PATH. If M$ ever install Java® by default again, you will have a newer version of javac than of the java tool. On a non‑Windows® box, you would have had some exceptions flying around by now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!