Forums Register Login

Compiling First Java Program in Command Prompt

+Pie Number of slices to send: Send
Hi everyone!

I'm trying to get the command prompt to run Java from a notepad saved file. I have a little programming background with Game Maker. One of my students wants to create Mine Craft mods so we're learning Java together.

We're going through the Java tutorials on their website: https://www.tutorialspoint.com/java/java_basic_syntax.htm

I've set the environment variable path to: C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin

I've created a folder in C:\Users\Ryan\Java and that's where I am in the command prompt. When I type java MyFirstJavaProgram I just keep getting this 'java' is not recognized as an internal or external command, operable program or batch file.

Any suggestions what I'm doing wrong?

I appreciate the help!

Ryan
+Pie Number of slices to send: Send
was your command prompt opened before you updated your path? If so, close it and open a new one.  Or, type "path" to see what that window thinks the path is, and confirm that directory is there.

navigate to the c:\Program Files\java\jdk\bin  and see if there really is a "java.exe".

try calling it with the full path:

\Program Files\java\jdk\bin\java.exe MyFirstJavaProgram

and let us know if any of those work.
+Pie Number of slices to send: Send
You were right, my path was wrong. The name of the folder was jdk1.8.0_131.

Thanks so much, it worked! I'm sure I'll have many more questions in the future!

Does this website have good learning tools/examples?
+Pie Number of slices to send: Send
Have you used the javac.exe command to compile the source file and create a class file?  Normally the JRE install sets the PATH variable so the java command works.
So I'm confused about an error getting to the java command.  The path would have been set or found for the javac command to have been executed.
+Pie Number of slices to send: Send
 

Ryan Daniele - Does this website have good learning tools/examples?



I don't think that they web site has any online tutorials, but many if you were to search this site I'm sure that you would find people mentioning and/or linking to sites on the web or books etc.
Java is also rather large with many different areas/specialties, much like programming itself. You may want to either open up your mind to narrow your focus, depending on what exactly you are planning on doing.
Some people have come to this web site for help with Java certification, others have come solely to help other people out and still others came post questions to problems (usually fairly specific) or to read and learn from the community posts.

If you are trying to get into Java then you may want to look into the book titled Head First Java.
Once you can navigate your way though the different loops (for, do, while) and decision constructs (if/else/switch) you may want to start learning methods, classes and then other object orientated programming.
Do not be discouraged when learning Java or programming for that matter. Nearly everyone started out printing something like "Hello World" to the screen and everyone makes mistakes, some more then others. It's though making mistakes that you learn (or hopefully learn eventually).
Programming is something that is usually easy to start, but difficult to master.
+Pie Number of slices to send: Send
Thank you so much for the advice. I will look into that book. I ran into a problem with the student, I did the exact same setup of everything on his computer as on mine but it ran into the same error. I'll work more on it next week.

Thanks again.
+Pie Number of slices to send: Send
It is usually easy to set up a computer to run Java®; here are three likely problems:-
  • 1: Failure to install Java® correctly in the first place.
  • 2: Failure to set the PATH correctly.
  • 3: Setting a CLASSPATH; it is usually more trouble than it is worth to have a system CLASSPATH at all.
  • +Pie Number of slices to send: Send
    Hi again,

    I'm having problems getting it to work on my students laptop. I'm using Windows 7, he's using Windows 10.

    I downloaded the java jdk-8u144-windows-x64.exe from this website on both computers but I changed the folder name to just jdk: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

    Both of our Environment Variable Paths are: C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin

    Both of our java folders are c:\Users\Java

    But when I type javac MyFirstJavaProgram.java for him, it says 'javac' is not recognized as an internal or external command, operable program or batch file

    I'm able to compile and run it on my computer just fine. I hope the pictures I attached are helpful.

    Thanks for the help,
    Ryan
    Student-Path.png
    Student's Environment Path
    [Student-Path.png]
    Student-CMD.png
    Student's CMD Log
    [Student-CMD.png]
    Student-JDK-Bin-Folder.png
    Student's jdk bin folder to show the java.exe
    [Thumbnail for Student-JDK-Bin-Folder.png]
    +Pie Number of slices to send: Send
    And the reason there's two paths of the same thing is because on his computer there was one that read:  C:\ProgramData\Oracle\Java\javapath

    But I changed it to match the path it should be, just to test it. It didn't work either way.
    +Pie Number of slices to send: Send
    Is the javac.exe file in the folder  on the PATH environment variable? (c:\Program Files\java\jdk\bin)  The OS uses the PATH to find executable files.

    Open a command prompt window and enter: PATH to see the contents of the PATH variable.

    C:\ProgramData\Oracle\Java\javapath


    That is the value the install of the JRE adds to the PATH.
    +Pie Number of slices to send: Send
    When you are in the command prompt and you type path you should see something like
    C:\Program Files\Java\jdk1.8.0_131\bin\
    This should be the location of the javac file on the system in question.
    When you get a chance please do that and paste the results in your reply.

    As well in your reply can you paste the results of the following command echo %java_home%
    You may not have a java_home defined, which is fine. This is not always needed.

    Please note that after you change the path, you may need to close the command prompt and open up a new one for the results to be noticed.

    Being a Windows system you can do either
    echo %java_home%
    or
    echo %JAVA_HOME%
    and you will get the same result.
    +Pie Number of slices to send: Send
    When I type path on my comp it shows it correctly, but on my students it shows all of the paths in the picture.

    So I have a feeling I need to delete or disable all of those extra paths. But they were already there and I'm afraid to delete things if I'm not sure if it will mess up anything else.

    Should I delete everything except the one for java?

    I'm not sure what the echo %java_home% did, after I typed it in it just said that same thing on the next line.

    Thanks!!
    Student-Path.png
    [Thumbnail for Student-Path.png]
    +Pie Number of slices to send: Send
     

    they were already there and I'm afraid to delete things if I'm not sure if it will mess up anything else.  


    I wouldn't change anything that you didn't enter.  The OS uses PATH to find executable files when the path to the file is not given.  There are probably programs on your PC that require PATH.
    I don't know what programs use the JAVA_HOME variable.  I don't think the JDK or JRE use it.  Maybe its used by some IDEs.

    It would be more useful if text were posted instead of images.  The contents of images can not be copied to include in responses.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    +Pie Number of slices to send: Send
    Right, I'm assuming it's there for a reason.

    And I'm assuming we need to get it to say what mine says when I type in path.


    MINE:

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

    C:\Users\Ryan>path
    PATH=C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin

    C:\Users\Ryan>



    STUDENTS:

    Microsoft Windows [Version 10.0.15063]
    (c) 2017 Microsoft Corporation. All rights reserved.

    C:\Users\Spence>path
    PATH="C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin";"C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin";C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Hewlett-Packard\SimplePass\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;"C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin";

    C:\Users\Spence>
    +Pie Number of slices to send: Send
    Are you still using Windows® Vista? I would consider upgrading Windows® if I were you.
    Do all your other programs still work? I would have anticipated problems finding programs at all if you are missing the other things which are in the PATH on the other computer.
    +Pie Number of slices to send: Send
    The positions of the "s are not consistent.  They should wrap paths that contain spaces.
    You show:
    PATH="C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin";"C:\WINDOWS\SYSTEM32; ...

    I think proper positions for the "s would be:
    PATH=C:\WINDOWS\SYSTEM32;"c:\Program Files\java\jdk\bin";C:\WINDOWS\SYSTEM32; ...



    Windows 7, Service Pack 1 6.1.7601
    +Pie Number of slices to send: Send
    when I try to put "s where you suggested, it gives me an error saying: A path component can't contain any control characters nor any of the following characters: / " < > |

    I'll check out that guide
    +Pie Number of slices to send: Send
    I'm using Windows 7, his computer is Windows 10, no Vista.
    +Pie Number of slices to send: Send
    If I was able to figure out how to edit posts I wouldn't make three new ones, but I can't find it.

    I set up the path following this tutorial website: https://www.tutorialspoint.com/java/java_environment_setup.htm
    +Pie Number of slices to send: Send
    To edit posts you need to get some cows. You can read about this here https://coderanch.com/wiki/659926/Ranch-Cows. Editing rules are subject to change.

    On my Windows 10 Pro system I have zero quotes in the path variable as noted here (get ready it's a long one):

    Note that in on line 12 I have the directory where javac is located and everything works fine for me.

    I know that I probably have more then you and more then what is needed.
    I just thought I'd show you mine to show you quotes are not needed for this to work.
    I think that the issue that you are experiencing is the quotes.
    If you take them out of yours or your students path you should be set I think, providing you are pointing to the correct directories that is.
    +Pie Number of slices to send: Send
    On Win10 when I display the value in PATH, some paths with embedded spaces are wrapped in "s and some are not.  There is never two paths within one pair of "s
    I tried adding a path to PATH with "s before and after the path and it worked, but the "s were stripped off when it was saved.

    I don't know what Windows is doing or why some paths are  wrapped in '"s and some are not.

    The problem with what you posted was that several paths were within one pair of "s instead only a single path within a pair of "s.
    I don't have any ideas why that could happen.

    Here is my PATH with a small edit to end each line with a ;

    PATH=C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;
    C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;
    C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;
    C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;
    C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;
    "C:\Program Files (x86)\MySQL\MySQL Fabric 1.5 & MySQL Utilities 1.5\";
    "C:\Program Files (x86)\MySQL\MySQL Fabric 1.5 & MySQL Utilities 1.5\Doctrine extensions for PHP\";
    C:\Program Files (x86)\Skype\Phone\;C:\BatchFiles;C:\Users\Norm\AppData\Local\Microsoft\WindowsApps;
    "C:\Program Files\Java\jdk1.8.0_60\bin\";

     
    +Pie Number of slices to send: Send
    The "s only show up when I'm in command prompt and type in Path. They don't show up in the environment variables. Strange?

    This is very odd. It seems like it should be super simple.

    The error

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

    +Pie Number of slices to send: Send
    I think I just got it! I deleted the c:\WINDOWS\System32 part so that it looked like yours: C:\Program Files\java\jdk\bin\

    and it worked!

    Maybe Windows 10 just needs to look a little different than my Windows 7...
    Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 3144 times.
    Similar Threads
    Help with set up for Head First Java
    Javac
    problem installing java
    Help! Java has not worked for years!
    Trouble running first application
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 28, 2024 07:58:33.