2

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]
User
  • 483
  • 4
  • 19
  • I didn't get, how do I do that, If you will go to my path output, java bin folder is there in the end, if that's what you mean. – User Nov 05 '15 at 00:48
  • Found a thread that said quotes in the path caused this problem. You could try modifying `sublime-build` – Michael Queue Nov 05 '15 at 00:55
  • @MichaelQuatrani without quotes it goes all red. But even then it shows the same error. – User Nov 05 '15 at 01:14
  • a flag is something you use after a command. When using the command line to compile a program you would say `javac myprogram.java` and that compiles the project into bytecode. Then you say `java myprogram.class` and you can run the program. There are flags that start like `javac -g` which means Generate debugging info. So, your program is telling you invalid flag for some reason. I'm not sure why, but, I hope that helps. Hint: switch to Ubuntu!!! – Michael Queue Nov 05 '15 at 01:18
  • @MichaelQuatrani Switching to Ubuntu won't help, this is caused by spaces in the file name, javac would exhibit the same behaviour on any platform. – Matt O Nov 05 '15 at 01:22
  • @MattO my custom build was not working due to the same reason, it has path for jre instead of jdk – User Nov 05 '15 at 02:06

2 Answers2

2

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.

Matt O
  • 1,336
  • 2
  • 10
  • 19
  • thank you very much, I never knew the difference is there between jre and jdk. I have been trying this for a week. Now am sure can get that working. But now the error changes to something different, included the same in post. – User Nov 05 '15 at 01:12
  • Do you have spaces in your file path? E.g. if your files are somewhere in My Documents, this error will happen. Try wrapping the filepath in quotes. – Matt O Nov 05 '15 at 01:20
  • finally found it, it was due to using `J` instead of `j` – User Nov 05 '15 at 01:52
2

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.

Community
  • 1
  • 1
User
  • 483
  • 4
  • 19