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

  • Java
  • Thread starter Math10
  • Start date
  • Tags
    Java
In summary: In the first two cases, you have to specify where a program is located each time. But, with the Path variable, the OS can find it for you if the program is in one of the directories specified in the Path.So, with a text editor like Notepad++, you would have to configure the path every time?No. just once.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
  • #1
Math10
301
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
  • #2
Seems to me that you didn't add the javac path to the PATH environmental variable!
 
  • #3
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
Then how to add the javac path to the PATH environmental variable?
 
  • #5
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.
 
  • #7
But what's the PATH info that I need to enter to the top section?
 
  • #8
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.
 
  • #9
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.
 

1. Why am I getting an error when I try to compile my Java code?

There could be a few reasons why you are getting an error while trying to compile your Java code. One possibility is that there is a syntax error in your code, such as a missing semicolon or curly brace. Another possibility is that you are trying to use a variable or method that has not been declared or defined. It is also possible that you are using the wrong Java compiler for your code. Double check your code for any errors and make sure you are using the correct compiler.

2. How can I fix a "class not found" error when trying to compile my Java code?

If you are getting a "class not found" error while trying to compile your Java code, it means that the compiler cannot locate a specific class that is being referenced in your code. This could be due to a missing or incorrect classpath setting. Make sure that all the necessary class files are in the correct location and that your classpath is correctly set up.

3. What does it mean when I get a "cannot find symbol" error while compiling my Java code?

A "cannot find symbol" error means that the compiler cannot find a particular variable, method, or class that is being referenced in your code. This could be due to a typo or misspelling in your code, or it could be because the variable, method, or class has not been declared or imported correctly. Double check your code for any errors and make sure that all necessary imports and declarations are present.

4. Why am I getting a "method not found" error when trying to compile my Java code?

A "method not found" error means that the compiler cannot find a certain method that is being called in your code. This could be due to a misspelling or incorrect method name, or it could be because the method has not been defined or imported correctly. Make sure that the method is spelled correctly and that it is being called on the correct object.

5. How can I troubleshoot "cannot resolve symbol" errors while compiling my Java code?

If you are getting "cannot resolve symbol" errors while compiling your Java code, it means that the compiler cannot find a particular symbol (such as a variable or method) that is being referenced in your code. This could be due to a typo or misspelling, or it could be because the symbol has not been declared or imported correctly. Double check your code for any errors and make sure that all necessary imports and declarations are present.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
3
Views
4K
  • Programming and Computer Science
Replies
3
Views
15K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
674
Back
Top