• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

My first java code MooseGreetings

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

I am very new to this java language coding,  I have installed  java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)

tried to write simple code using example from this forum the compile it.

here is what I did

C:\Java\src>java MooseGreetings.java
Error: Could not find or load main class MooseGreetings.java

I don't know why it happened like that. please help

thank you
 
Yedija Tjin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when I used

C:\Java\src>javac MooseGreetings.java
'javac' is not recognized as an internal or external command,
operable program or batch file.


 
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to run the app from the java folder, please.

Just go one level up.. / and try it
 
Yedija Tjin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Angus Ferguson wrote:Try to run the app from the java folder, please.

Just go one level up.. / and try it



still doesn't work, give me the same result.

Question: do I need to put my java file on the folder where I install jdk ? (I tried to save in the same folder but still doesn't work)

 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Javac is in the bin folder. You can try to run it from there but it is not a good practice.

Do you know what the classpath is? And cintext variables like java home?
 
Yedija Tjin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to uninstall everyting (java jdk) and install it again. now it gave me something.

when I run C:\Java\src>java MooseGreetings.java

it created MooseGreetings.class

but when I run it, it gave me like

C:\Java\src>java beerSong1
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: beerSong1 has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
       at java.lang.ClassLoader.defineClass1(Native Method)
       at java.lang.ClassLoader.defineClass(Unknown Source)
       at java.security.SecureClassLoader.defineClass(Unknown Source)
       at java.net.URLClassLoader.defineClass(Unknown Source)
       at java.net.URLClassLoader.access$100(Unknown Source)
       at java.net.URLClassLoader$1.run(Unknown Source)
       at java.net.URLClassLoader$1.run(Unknown Source)
       at java.security.AccessController.doPrivileged(Native Method)
       at java.net.URLClassLoader.findClass(Unknown Source)
       at java.lang.ClassLoader.loadClass(Unknown Source)
       at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
       at java.lang.ClassLoader.loadClass(Unknown Source)
       at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)



 
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Angus sugested, my guess is you haven't set your enviorement variables, just click your windows button and start typing enviorement, it's somewhere in your configurationscreen, but I don't know where exact, when you have found them create a new enviorementvariable called JAVA_HOME and point it to (copy the path) your javafolder. Now you have to update your PATH or path (whatever it is for you, if you don't have one, what is verry unlikely, create one) variable.
Go to the verry end of the content of the variable add a ; and then %JAVA_HOME%\bin and you should be good to go. To check if you have set them right you can execute Echo %JAVA_HOME% in your cmd, it should return the path to the installation folder of your java. You also don't need the extention, go to your programs dir and run java moosegreetings
 
Marshal
Posts: 79278
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First things first:

Welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 79278
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoever said you have set up your environment variables wrongly is, I am sure, correct.
Please atart by showing us your variables: please show us what happens when you pass the following three instructions to the command line:-

when I run C:\Java\src>java MooseGreetings.java

it created MooseGreetings.class

No, it didn't; in order to create XXX.class you have to use the javac command.

Once we know what your environment variables are set to, we can help you correct the problem.

Why have you got a java directory inside the root of C? I usually suggest you create a java directory inside your User\XXX folder:-You only need line 1 once per computer, then use line 2 to navigate to that directory whenever you open the command prompt.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is everyone always talking about the classpath variable, I know what it does, but I never, in the three years I program and the dozen computers I installed java on, needed to add or change the classpath variable
 
Campbell Ritchie
Marshal
Posts: 79278
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:. . . classpath variable, I know what it does . . .

It completely messes up your programming by making it impossible to find the requisite files, that is what it does

Unfortunately there are still old books around telling you to set a system CLASSPATH, so we need to know what the user has done.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah ok, I misunderstood, I thought you were asking him what he had done to see if it's set correctly, but if I'm not mistaking it's program specific no? So by setting it now, you would have to reset it your next programm and again, and again, and ...
 
Yedija Tjin
Greenhorn
Posts: 8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for all your help

after reading all your help I go thru all the step again. this is what I did wrong
1. when I set my path I put ; on %JAVA_HOME%\bin; (I am using windows 10 )  
2. I install mix version jdk version 8 and 11 somehow it didn't work, so what I did was I uninstall everything and just install jdk ver 11

thank you once again. looking forward to learn from you all

 
Yedija Tjin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Daniel Demesmaecker wrote:. . . classpath variable, I know what it does . . .

It completely messes up your programming by making it impossible to find the requisite files, that is what it does

Unfortunately there are still old books around telling you to set a system CLASSPATH, so we need to know what the user has done.



Another Question, do we need to set classpath or not ???

is this stepp still required ??
Setting the JAVA_HOME and PATH environment variables

Windows XP

Start/Settings/Control Panel.
Double-click System, and select the Advanced tab.
Click the Environment Variables button.
You can set environment variables for either your user only, or for all users (System variables). But don't set a user JAVA_HOME and a system PATH (see this discussion). Add a new variable in either of them by clicking the New button.
Set the variable name as JAVA_HOME, and its value to the directory where you installed the JDK (e.g. C:\Program Files\java\jdk1.8.0_172)
Click OK.
Look for a variable called PATH (the name might slightly vary, for example 'Path'). Select it and click Edit. At the beginning of its value, add the following : "%JAVA_HOME%\bin;" (without the double quotations). Don't forget the semi-colon at the end, to separate this directory to the other directories already in the path.
Click OK and close all remaining windows.

thanks
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No you don't need to se the classpath, have you echoed your variables to see if they're set correctly?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yedija Tjin wrote:after reading all your help I go thru all the step again. this is what I did wrong...


Thumbs up for posting your solution back to the forum!
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Normally you can have different jdk's installed, on my workcomputer I have three, you can however only have 1 %JAVA_HOME%, that is also the done you should at to your path variable
 
Campbell Ritchie
Marshal
Posts: 79278
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yedija Tjin wrote:Thank you

That's a pleasure

. . .  2. I install mix version jdk version 8 and 11 somehow it didn't work, so what I did was I uninstall everything and just install jdk ver 11 . . .

Since you haven't shown us your PATH, we don't know that you have set it correctly. It might have been possible to correct your problem by editing the PATH, so, as Daniel Desmaecker said, you can install multiple different Java® versions.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic