Java [Java] How come I can't compile this?

  • Thread starter Thread starter Math10
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
The user is unable to compile a Java application using the command prompt due to the 'javac' command not being recognized. The discussion reveals that the issue likely stems from not having the Java Development Kit (JDK) installed or the path to 'javac' not being set in the system's PATH environment variable. Instructions are provided for adding the JDK path in Windows, emphasizing the importance of ensuring the correct directory is included. The conversation shifts towards the potential benefits of using a Java-specific IDE, which would simplify the compilation process. Ultimately, verifying the correct setup of the PATH variable is crucial for successful compilation.
Math10
Messages
301
Reaction score
0
I wrote a very simple Java application in Notepad++ and saved it in the Documents inside of File Explorer. Then I opened the command prompt and typed javac GetStarted.java but it says 'javac' is not recognized as... So how do I compile this?
 
Technology news on Phys.org
Seems to me that you didn't add the javac path to the PATH environmental variable!
 
Note also that javac is not part of the Java Runtime Environment (JRE) that is installed on most computers. You have to have the Java Development Kit (JDK) installed and have your path pointed at it's bin directory where javac is installed as Shyan stated above.
 
Then how to add the javac path to the PATH environmental variable?
 
Math10 said:
Then how to add the javac path to the PATH environmental variable?
Assuming that you have Windows. Go to Control Panel. Select System and then Advanced System Settings. From there you will get a dialog with 5 tabs. Select the Advanced tab and Environment Variables button on the bottom. That will bring up a dialog with two sections. The bottom section has administrative values that apply to every user. If you don't have admin rights, you can enter the path info on the top section but you still need to know what you're doing.
 
But what's the PATH info that I need to enter to the top section?
 
Math10 said:
But what's the PATH info that I need to enter to the top section?
You have to figure out where the executable you want the execute IS and add a path to that folder.
 
Math10 said:
But what's the PATH info that I need to enter to the top section?
If you don't have admin access, you can update your path by creating a new Path variable in the top section and setting the value to the directory where your javac program is located. My javac.exe is located in C:\Java\jdk1.7.0\bin so it would look like this:

Path.png


Enter the path for your javac and it should work. This will append your user Path to the Path that all users get in the lower section. Once you have added the javac path, you can confirm that it has been set by opening a command prompt and typing the word 'set'. This will show you all of the variables that have been set for your system. You should see a PATH variable with a good number of directory paths separated by semicolons. Your new javac path should be at the end. You should also be able to type javac at this point and it will display the program's run options.
 
  • #10
After I typed 'set', I did see lots of things. But when I typed javac GetStarted.java, it still says 'javac' is not recognized as...
 
  • #11
Math10 said:
After I typed 'set', I did see lots of things. But when I typed javac GetStarted.java, it still says 'javac' is not recognized as...
Was the directory where your javac is located part of the Path?
 
  • #12
I guess so.
 
  • #13
Math10 said:
I guess so.
I guess so? Really? I can't help if you can't even verify that.
 
  • #14
But you know what? This is too complicated. I think I should get a Java-specific IDE rather than doing it with Notepad++. Because Notepad++ is just a text editor, I have to do this path thing every time I need to compile something. So What Java-specific IDE would you guys recommend?
 
  • #15
Math10 said:
But you know what? This is too complicated. I think I should get a Java-specific IDE rather than doing it with Notepad++. Because Notepad++ is just a text editor, I have to do this path thing every time I need to compile something. So What Java-specific IDE would you guys recommend?
You will have to have your path configured with an IDE also. Once it is set, you won't have to set it again.
 
  • #16
So with a text editor like Notepad++, you would have to configure the path every time?
 
  • #17
Math10 said:
So with a text editor like Notepad++, you would have to configure the path every time?
No. just once.
 
  • #18
Think about this. How does an Operating System (OS) like Windows know where a program is located when you want to run it? There are three ways - you can find the program yourself and run it from that location, you can run the program from another location by entering the path (or relative path) to the program or, as we've been trying to show you, the OS can find it for you if the program is in one of the directories specified in the Path.

The Path variable tells the OS where to look for programs that it can run without having to specify where those programs are located. In the first two cases, you have to specify where a program is located each time. But, with the Path variable, the OS will find it for you. This is why we've been trying to help you to set up the location of javac in your path. This is an important thing that you're going to have to know and understand if you're going to learn programming.
 
  • #19
So what's the problem? I wrote my Java application and saved in the Documents inside File Explorer, I'm using Windows 8. Then I go to the Computer, Properties, System, Advanced System Settings, Environment Variables, in the top section, I typed Path, with C:\Program Files\Java\jdk1.8.0_40\bin and I opened the command prompt and typed javac GetStarted.java but it says 'javac' is not recognized as...
 
  • #20
Is javac.exe located in your C:\Program Files\Java\jdk1.8.0_40\bin directory?

P.S. I've got to leave for an hour.
 
  • #21
No, I don't see javac.exe in bin, only javac.
 
  • #22
What's javac.exe by the way?
 
  • #23
Math10 said:
What's javac.exe by the way?
javac.exe is javac. Microsoft has a setting that, by default, hides known extensions like .exe (I hate that stupid setting).
Try this. Open a command prompt and run these two commands:
cd "C:\Program Files\Java\jdk1.8.0_40\bin"
javac

For each command, type the command and hit Enter to run it. The first command will change directory (cd) to the place where you've said that javac is located. The quote marks are necessary because of the space in the Program Files name. You might have to manually type the quote marks. Also, make sure that there is a space after cd.

For the second command do not try to run your program. Just type javac on the second line and hit Enter. You should get a list of options for how to run javac. What do you get when you run it?
 
Last edited:
  • #24
You should see something like this:

C:\Program Files\Java\jdk1.8.0_40\bin>javac
Usage: javac <options> <source files>
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath <path> Specify where to find user class files and annotation processors
-cp <path> Specify where to find user class files and annotation processors
-sourcepath <path> Specify where to find input source files
-bootclasspath <path> Override location of bootstrap class files
-extdirs <dirs> Override location of installed extensions
-endorseddirs <dirs> Override location of endorsed standards path
-proc:{none,only} Control whether annotation processing and/or compilation is done.
-processor <class1>[,<class2>,<class3>...] Names of the annotation processors to run; bypasses default discovery process
-processorpath <path> Specify where to find annotation processors
-d <directory> Specify where to place generated class files
-s <directory> Specify where to place generated source files
-implicit:{none,class} Specify whether or not to generate class files for implicitly referenced files
-encoding <encoding> Specify character encoding used by source files
-source <release> Provide source compatibility with specified release
-target <release> Generate class files for specific VM version
-version Version information
-help Print a synopsis of standard options
-Akey[=value] Options to pass to annotation processors
-X Print a synopsis of nonstandard options
-J<flag> Pass <flag> directly to the runtime system
-Werror Terminate compilation if warnings occur
@<filename> Read options and filenames from file
 
  • #25
Yes, I got that! Now what do I do?
 
  • #26
OK, that's good. Now, if you can type javac from another directory and have it work the same, then you know that it's in your PATH correctly. If not, then you will have to go back and try to set it again. You haven't specified your OS but, the link in post # 6 by Shyan has instructions for different systems. Once you can get the system to recognize javac from a different directory, then you're ready for the next step.
 

Similar threads

Back
Top