Post a Job  |   Ask a Question?   |   Already registered? login or register
durgapratap satyamsetti
23/05/2015 02:56 AM

EveryBody Please check this and try

Place this file in any drive ------------ package java.lang; public class String{ public String toString(){ return "my own string"; } But dont compile ok And place the below file in same drive ------- public class A{ public static void main(String[]args){ System.out.println("from A"); } } Now complie this file (remember we should not compile Now it shows like below error error: cannot access String public static void main(String[] args) ^ bad source file : .\ file does not contain class String please remove or make sure it appears in the correct subdirectory of the sourcepath Why i am getting this error ? I think even compile time it also checks which string class has to taken whether from rt.jar or from my classpath.. But it even it checks in .java files also (remember we did'nt file) Once try this and comment your o/p and tell your opinion I have my own opinion (may or may not right) But i want your opinion also.
Spam    2 Comments

23/05/2015 04:26:16
yes,pratap i tried this,iam also getting the same problem.i think u r correct...

23/05/2015 08:47:59
I have tried ur example error is correct, this is my openion... First of all rt.jar never contains .java files Javac compiler by default checks for .java in current directory and i don't know reason its also checks in directory java/lang(May be java.lang is package).and we can give -cp or -classpath to javac to check even in others directiries as well coming to ur example In ur String class package decleration as java.lang javac While compiling it finds String and searches for in current directory and it finds and it tries to compile while compiling it finds package java.lang and we didnt explicitely mention where to create(i.e., javac -d . Since is find when we are trying to compile so it checks weather .java is placed in an appropriate directry(means java\lana\ so,it give and error like (Please remove or make sure it appears in the correct subdirectory of the classpath) to compile ur code try this | |-java |-lang | now javac and then run java A now while compiling it compiles ur and also creates String.class in directory java/lang/String.class But while Running A.class it Loads Predefined String (i.e., rt.jar String.class) why its loads predefined String we know already (i.e., Principle of Delegation) this my Openion,if iam wrong correct me........

Languages :