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

  • Context: Java 
  • Thread starter Thread starter Math10
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around issues related to compiling a simple Java application using the command line. Participants explore the necessary setup for the Java Development Kit (JDK), specifically the configuration of the PATH environment variable to recognize the 'javac' command.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant reports an issue with the command prompt not recognizing 'javac' when attempting to compile a Java file.
  • Another participant suggests that the problem may stem from not having the javac path added to the PATH environment variable.
  • It is noted that 'javac' is part of the JDK, not the Java Runtime Environment (JRE), and that the JDK must be installed for compilation.
  • Several participants provide instructions on how to add the javac path to the PATH environment variable, with varying levels of detail.
  • There is a repeated inquiry about what specific PATH information needs to be entered, indicating some uncertainty among participants.
  • One participant expresses frustration with the complexity of the setup and suggests switching to a Java-specific IDE, while others clarify that IDEs also require path configuration.
  • Participants discuss the visibility of file extensions in Windows, with one explaining that 'javac' is actually 'javac.exe' but may not be displayed due to system settings.
  • Instructions are given to verify the location of 'javac' by changing the directory in the command prompt and running the command directly.

Areas of Agreement / Disagreement

Participants generally agree on the necessity of configuring the PATH variable to use 'javac', but there is no consensus on the specific steps or the ease of the process. Some participants express confusion and frustration, while others provide technical guidance.

Contextual Notes

There are limitations in the discussion regarding the clarity of instructions for setting the PATH variable, as well as the assumption that all participants have the same level of familiarity with system settings and command line operations.

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

  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
7K
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
15K
Replies
17
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K