Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

I am failing to execute code

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings.

I am trying to execute this code:



Attached is the cmd window showing how when i try execute it just goes to next line.
Capture.PNG
[Thumbnail for Capture.PNG]
 
Saloon Keeper
Posts: 6079
154
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

You're not executing it, you're just compiling it. Use "java greetings" to execute it. But that will not work, because the class is not public - so you need to make it public.

In the future, please do not post screenshots - they can't be quoted, and they can't be searched. Post the actual text instead.
 
Tatenda Mawoneke
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i changed the code and added the public for class :



the file is saved in "C:\Users\mawoneket\Documents\Java Scripts" and it it saved as "greetings"

in command prompt i have used "C:\Users\mawoneket\Documents\Java Scripts>javac greetings.java" and this just jumps to the next line

when i use "C:\Users\mawoneket\Documents\Java Scripts>javac greetings" i get the message saying "error: Class names, 'greetings', are only accepted if annotation processing is explicitly requested
1 error"

Any thoughts on my errors?
 
Tim Moores
Saloon Keeper
Posts: 6079
154
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"javac" is for compiling code, not for executing code. Re-read my previous post carefully - it tells you how to execute code.
 
Tatenda Mawoneke
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:"javac" is for compiling code, not for executing code. Re-read my previous post carefully - it tells you how to execute code.



Ok i see, my problem is solved thank you!
 
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tatenda Mawoneke wrote:. . . and this just jumps to the next line . . .

That means your javac tool is running with no error messages.

And welcome to the Ranch again.
 
Tatenda Mawoneke
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you i was about to google differences between typing java and javac.
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This link tells you what javac and java do. It probably contains lots more information than you require.
 
Tatenda Mawoneke
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you can tell me something also...

I installed " Java SE Development Kit 8u241" and also " Java SE Development Kit 13.0.2" and made a "environment variable" that goes to the bin for java...all this i saw on a video for getting started with java. Can you explain what the purpose of this would be?
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us what happens when you execute these two commands:-

java -version
javac -version

That tutorial is no better than many on YouTube; I can see several things I am not happy about:-
  • 1: It is out of date, using Java6 and Win7. On Win10 you have to create a separate PATH entry in a separate line for each program you want to run.
  • 2: He is creating a user Path; I think it would be better to add the PATH to Java® to the beginning of the existing system PATH.
  • 3: He calls the long error message after writing javac “crap”. It isn't crap at all.
  • 4: The Java® code is incorrectly indented.
  • 5: It is poor style to have [] after “args”. The [] belong after “String”.
  • 6: It is poor style to have an xyz.java file and a class name starting with a lower‑case letter.
  • 7: I would prefer to have the \Java folder elsewhere than the root of a drive.
  • 8: He neither explains what he is doing with the PATH, nor that the installation window shows the correct PATH to add. See that video at 2′ 41″.
  • If you are running Java11+, you can delete the xyz.class file and run your code by writing java Greetings.java Remember to give the class name a capital G.

    The idea of the PATH is that it guides the OS where to find the programs called javac and java; they are both in the same PATH. It looks through all the PATH entries in order until it finds a program matching the requested name, e.g. java.
     
    Tatenda Mawoneke
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Please show us what happens when you execute these two commands:-

    java -version
    javac -version

    That tutorial is no better than many on YouTube; I can see several things I am not happy about:-
  • 1: It is out of date, using Java6 and Win7. On Win10 you have to create a separate PATH entry in a separate line for each program you want to run.
  • 2: He is creating a user Path; I think it would be better to add the PATH to Java® to the beginning of the existing system PATH.
  • 3: He calls the long error message after writing javac “crap”. It isn't crap at all.
  • 4: The Java® code is incorrectly indented.
  • 5: It is poor style to have [] after “args”. The [] belong after “String”.
  • 6: It is poor style to have an xyz.java file and a class name starting with a lower‑case letter.
  • 7: I would prefer to have the \Java folder elsewhere than the root of a drive.
  • 8: He neither explains what he is doing with the PATH, nor that the installation window shows the correct PATH to add. See that video at 2′ 41″.
  • If you are running Java11+, you can delete the xyz.class file and run your code by writing java Greetings.java Remember to give the class name a capital G.

    The idea of the PATH is that it guides the OS where to find the programs called javac and java; they are both in the same PATH. It looks through all the PATH entries in order until it finds a program matching the requested name, e.g. java.



    So for java version:



    and for javac version:



    note: I am using a book called "Head First Java" and i am just getting into it however it just says you need java sdk and a notepad but doesn't show how sdk work or what it is...it just shows the scripts and how to write them. So the video there is just the one i happened to have watched did not know it is not so good.

     
    Campbell Ritchie
    Marshal
    Posts: 67533
    257
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Head First Java? Even at fifteen years old, it is probably my favourite beginners' book. Don't try to run anything on objects from the Party class.
    SDK is an old name for the JDK; the version numbers show that you have successfully run Java8. If you want Java13, try the following instruction at the command line, but I have probably got some of the folder names wrong:-Beware: code compiled with Java13 won't usually run on older versions.
    Don't use MS notepad, but download Notepad++, a much better app. The video showed Notepad++ (a good thing) but it wasn't named clearly.
     
    Tatenda Mawoneke
    Greenhorn
    Posts: 16
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Head First Java? Even at fifteen years old, it is probably my favourite beginners' book. Don't try to run anything on objects from the Party class.
    SDK is an old name for the JDK; the version numbers show that you have successfully run Java8. If you want Java13, try the following instruction at the command line, but I have probably got some of the folder names wrong:-Beware: code compiled with Java13 won't usually run on older versions.
    Don't use MS notepad, but download Notepad++, a much better app. The video showed Notepad++ (a good thing) but it wasn't named clearly.



    Ok i will continue with this java version then and proceed to use notepad++.
    Thanks.
     
    Campbell Ritchie
    Marshal
    Posts: 67533
    257
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's a pleasure
     
    What's a year in metric? Do you know this metric stuff tiny ad?
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!