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

  • Java
  • Thread starter Math10
  • Start date
  • #1
301
0

Main Question or Discussion Point

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?
 

Answers and Replies

  • #2
2,788
586
Seems to me that you didn't add the javac path to the PATH environmental variable!
 
  • #3
Borg
Science Advisor
Gold Member
1,871
2,261
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.
 
  • #4
301
0
Then how to add the javac path to the PATH environmental variable?
 
  • #5
Borg
Science Advisor
Gold Member
1,871
2,261
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.
 
  • #7
301
0
But what's the PATH info that I need to enter to the top section?
 
  • #8
phinds
Science Advisor
Insights Author
Gold Member
2019 Award
15,897
5,559
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.
 
  • #9
Borg
Science Advisor
Gold Member
1,871
2,261
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
301
0
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
Borg
Science Advisor
Gold Member
1,871
2,261
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
301
0
I guess so.
 
  • #13
Borg
Science Advisor
Gold Member
1,871
2,261
I guess so.
I guess so? Really? I can't help if you can't even verify that.
 
  • #14
301
0
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
Borg
Science Advisor
Gold Member
1,871
2,261
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
301
0
So with a text editor like Notepad++, you would have to configure the path every time?
 
  • #17
Borg
Science Advisor
Gold Member
1,871
2,261
So with a text editor like Notepad++, you would have to configure the path every time?
No. just once.
 
  • #18
Borg
Science Advisor
Gold Member
1,871
2,261
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
301
0
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
Borg
Science Advisor
Gold Member
1,871
2,261
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
301
0
No, I don't see javac.exe in bin, only javac.
 
  • #22
301
0
What's javac.exe by the way?
 
  • #23
Borg
Science Advisor
Gold Member
1,871
2,261
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
Borg
Science Advisor
Gold Member
1,871
2,261
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
301
0
Yes, I got that! Now what do I do?
 

Related Threads for: [Java] How come I can't compile this?

  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
3
Views
4K
Replies
2
Views
1K
  • Last Post
Replies
1
Views
376
  • Last Post
2
Replies
30
Views
4K
Replies
3
Views
601
Top