If you have multiple java files and all of them are in different packages. Then how can you compile them and run main class among them. Let me take an example
Consider two classes in a package com.test.
com.test.MainClass
com.test.UtilityClass
Compilation: If you go inside and compile using javac mainclass, you will be thrown an error
MainClass : Cannot find location
Similarly if you have class files in a package and if you try to execute,using java mainclass command, then system will throw following error.
e: com/test/<ClassName>)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Problem here is we go inside the folder com/test and try to compile or run files. But
Never go inside packaged folder(com or test) and try to run the files.
Come out of the package folder and run the commands.
Compilation:
javac ./com/test/MainClass.java
javac ./com/test/Utilities.java
Execution:
java com.test.MainClass.java
That really helps. Thank you...
ReplyDelete