-4

I am new at Java. I have installed my JDK on C:\Java\jdk1.8.0_131. When I am in the C:\Java\jdk1.8.0_131 this directory and execute my java program, it is ok. It runs. But when I am in the different directory it does not run.This error appears 'javac' is not recognized as an internal or external command, operable program or batch file. May be I added env variables to path not correctly? How can I solve it?

Holger
  • 285,553
  • 42
  • 434
  • 765
alakbear
  • 5
  • 3
  • 1
    I think you need to configure your `JAVA_HOME` environment variable. After this, `javac` (and `java`) should be accessible everywhere. – Tim Biegeleisen May 08 '17 at 07:00
  • 4
    You need to set PATH variable for diff directory . go to your enviroment variable and set path their – Abhi Andhariya May 08 '17 at 07:01
  • how can I do this? firstly I define a varible called JAVA_HOME that holds the address to java? – alakbear May 08 '17 at 07:02
  • To *run* a Java program, you should not need `javac`, as that’s the tool for *compiling* a program, which you don’t need to repeat for every execution. It is not surprising that it can’t be found without a proper path setup, however, it’s surprising that, whatever you do, is working when your current working directory is `C:\Java\jdk1.8.0_131`, as `javac` is not directly contained in that directory. It seems, you didn’t post the entire context. Besides that, I’m going to remove the `java-7` and `jdk1.6` tags, as you didn’t specify to have any Java installation of these versions… – Holger May 08 '17 at 12:06

3 Answers3

0

You need to set your java path. Please refer this link java.com

Thush-Fdo
  • 506
  • 1
  • 9
  • 28
0

You may try this. Include your JDK path up to and including /bin in the environment path properties in system environment which will be in control panel.

jwenting
  • 5,505
  • 2
  • 25
  • 30
  • Best is in fact to set a JAVA_HOME environment variable and use $JAVA_HOME/bin in the PATH environment variable. – jwenting May 08 '17 at 07:33
  • @jwenting: it seems, a lot of Java developers are overestimating the relevance of the `JAVA_HOME` variable. Java itself doesn’t use that variable. I never set that variable in the last twenty years… – Holger May 08 '17 at 12:09
  • @Holger It's mostly a convenience thing, allowing you to keep the $PATH definition short. And some tools do use it, things like app servers and some build tools for example in order to find the JDK. – jwenting May 09 '17 at 06:24
0

You need to add the Java bin path to the system environment variable so that the system could recognize the java command from other directory as well.

go to -> My Computer -> System Properties -> Advanced System Settings

Click on the bottom right part of the new opened dialog box: Environment Variables

Add C:\Java\jdk1.8.0_131\bin in the path of the system variables.

Restart the system after saving the changes.

Derrick
  • 3,669
  • 5
  • 35
  • 50
srp321
  • 116
  • 2
  • 10