1

My question is about one of the basic java command "javac". I have a ".java" file on my desktop.I have installed JDK 7 and added path variable to the environment variables. Here is the screenshot of the "environment variables". http://s13.postimage.org/n0oc89gp3/Untitled.png

When I'm working in C:\Users\JOXWH\Desktop there is no problem.Such as; If I try this code in here javac JavaApplication2.java. I'm having .class file on my desktop. But if I try to use javac from another directory with -cp parameter (like javac -cp C:\Users\JOXWH\Desktop JavaApplication2.java) I'm taking an error like this: http://postimage.org/image/4aps9ntlr/

pppery
  • 3,731
  • 22
  • 33
  • 46
ACrescendo
  • 46
  • 10

2 Answers2

1

no need of -cp javac C:\Users\JOXWH\Desktop JavaApplication2.java

will work

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42
  • yes that code can work "javac C:\Users\JOXWH\Desktop\JavaApplication2.java" .But what is the problem with -cp? – ACrescendo Dec 01 '12 at 10:21
  • 1
    you should specify class path after -cp, for eg: javac -cp . JavaApplication.java will work as it sets current directory as classpath. (dot means current dir) – Subin Sebastian Dec 01 '12 at 10:24
  • I think you're wrong there.it is possible to use it with javac. here is javac options :http://s9.postimage.org/y1uvinqu7/Untitled.png – ACrescendo Dec 01 '12 at 10:27
  • 1
    you can use -cp with javac but you should mention a path after that – Subin Sebastian Dec 01 '12 at 10:28
  • So how can ı edit this code, I think it is including path after -cp parameter :"javac -cp C:\Users\JOXWH\Desktop JavaApplication2.java" – ACrescendo Dec 01 '12 at 10:30
  • suppose your MainApplicaton2.java requires a external class to compile, in such cases you can use -cp option otherwise no need to use. javac -cp classes-dir C:\Users\JOXWH\Desktop JavaApplication2.java, then javac will lookup classes-dir to look for dependencies – Subin Sebastian Dec 01 '12 at 10:34
0

-cp tells the compiler where to look for other classes that the .java file(s) you are compiling depend on, not where to look for the java source files themselves. You need to give the appropriate paths to the java files in full.

In all but the most trivial cases the source file locations and the required class locations will be different, e.g.

javac -cp classes;lib\* -d classes src\com\example\MyClass.java

would take a .java file from under src which depends on classes under classes and on classes in jar files under lib, compile it, and put the resulting class file under classes in a directory matching the class's package name.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183