User
User

Reputation: 483

Sublime can't find the javac command

My Java bin is added to the path, still it's not able to find the javac I think.

The thing works fine in eclipse IDE.

public class testing {
    public static void main(String[] args) {

        System.out.println("hi");
    }
} 

Below is the error from sublime text 2. Removed some intel path otherwise path was too long.

[Error 2] The system cannot find the file specified
[cmd:  [u'javac', u'C:\\Users\.......\testing.Java']]
[dir:  C:\Users.....
[path: C:\ProgramData\Oracle\Java\javapath;C:\Python27\;C:\Python27\Scripts;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Java\jre1.8.0_45\bin;]
[Finished]

If I run it in Sublime text 3 then the error is below.

'javac' is not recognized as an internal or external command,
operable program or batch file.
[Finished in 0.0s with exit code 1]

I even tried the custom build but the error shown is same, below is the custom build.

MyJava.sublime-build

{
    "cmd": ["javac", "$file_name", "&&", "java" ,"$file_base_name"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "path" : "C:\\Program Files\\Java\\jre1.8.0_45\\bin",
    "selector": "source.java",
    "shell":true
}

Please help due to this I haven't been using Sublime text and continuing with Eclipse but thought let me try again.

Below is my Java folder. As suggested I added jdk to the path.

enter image description here

But now error changes to below.

javac: invalid flag: C:\........GitHub\HackerRank\testing.Java
Usage: javac <options> <source files>
use -help for a list of possible options
[Finished in 0.3s with exit code 2]

Upvotes: 2

Views: 9091

Answers (2)

User
User

Reputation: 483

First of all don't skip the details of how to build and run your program. In eclipse it happens by default that I wasn't even aware of path/javac/java etc.

Here are some great details on this What does "Could not find or load main class" mean?

  1. Use j not J in file names. ( test.java)
  2. javac test.java is used to compile the program which results in test.class compiled program.
  3. Then you use java test to run your program not java test.class
  4. Watch carefully your path for bin should be for jdk and not jre
  5. If you are running program in sublime text then ctrl+b will only compile the program and not run it.
  6. To do that same you can use the below custom build with your specific path details.

Myjava.sublime-build

{
    "cmd": ["javac", "$file_name", "&&", "java" ,"$file_base_name"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "path" : "C:\\Program Files\\Java\\jdk1.7.0_79\\bin",
    "selector": "source.java",
    "shell":true
}

This will both compile and run the program at same time.

Upvotes: 2

Matt O
Matt O

Reputation: 1346

Your path has the jre (Java Runtime Environment) bin directory, but no jdk (Java Development Kit), which is where javac is typically found. You'll need to find where your JDK lives and add that directory to your path as well.

Upvotes: 2

Related Questions