2

I have this confusion regarding the classpath and sourcepath.I have referred to theselinks too but i aint getting it right. REFERENCES--

Differences between classpath and sourcepath options of javac

-sourcepath vs -classpath

Regarding automatic recompilation, I can't spot the difference between javac's -classpath and -sourcepath options

Does it mean if we dont edit the source file,both classpath and sourcepath wouldnt be recompiled? Specifically,this line troubled me! Note that classes found through the classpath are subject to automatic recompilation if their sources are found. Can anyone explain this in simple language with SIMPLE EXAMPLES?It would be of great help!

Community
  • 1
  • 1
newuser
  • 31
  • 4
  • 1
    When you compile a Java program with `javac` the sourcepath is referenced to find other .java files the compiler needs. When you run a Java program with `java`, the classpath is referenced to find the .class files to be executed. If you want to supply already-compiled programs to `javac` when compiling (so it can resolve references in those), you also specify a classpath. – Hot Licks Jun 28 '14 at 19:23
  • Thanks for answering sir but could i request for a complete answer? – newuser Jun 28 '14 at 19:48
  • @HotLicks But why would the compiler javac, look for other .java files? I can understand javac looking for the files passed to it as parameters, and for any compiled classes that those files use, but why other java files? – barlop Aug 03 '14 at 22:55
  • @barlop - Well, if you declare a String, *javac* needs to look a String.class to find out what a String is. If there is no String.class it will look for String.java and compile it, in order to use the resulting .class file. – Hot Licks Aug 04 '14 at 02:33
  • @HotLicks Looks like -cp / -classpath, does too http://pastebin.com/raw.php?i=wB17PFEM – barlop Aug 04 '14 at 09:51
  • this may help http://stackoverflow.com/questions/2441760/differences-between-classpath-and-sourcepath-options-of-javac – barlop Sep 23 '14 at 12:57

2 Answers2

6

The sourcepath is the path to the sources you are compiling.

The classpath is a path (or multiple paths) to libraries you are compiling against. These are compiled classes, either in folders or Jar files.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • Thanks for answering,but could you tell me what's with the following thing discussed about automatic recompilation.."javac example command should use -classpath, while the other should not. When the javac example that includes -classpath is used, automatic recompilation occurs if the edited superclass source file is found. When the javac example that excludes -classpath is used, automatic recompilation does not occur if the superclass source file has been edited. " – newuser Jun 28 '14 at 19:45
  • I don't know about this, but it's a scenario you will rarely encounter in real life. Ideally, you don't interact with javac directly anyway, but let your IDE and/or build system take care of that for you – Sean Patrick Floyd Jun 28 '14 at 19:50
  • Yeah,i know but that pestered me for a while thanks anyway :) – newuser Jun 28 '14 at 19:51
0

Sourcepath tells javac where to grab the source code from. Classpath tells javac where to grab other class files that will allow your source code to compile.

Prakash
  • 53
  • 4
  • Can you give an example 'cos in this example, -sourcepath doesn't seem to do that. http://pastebin.com/raw.php?i=prtRjYL7 – barlop Aug 03 '14 at 22:53