0

I have placed a java file and compiled it with out a main method in JRE/lib/ext directory. I want to access the file from a local Java file with out even mentioning about importing it in java.

C:\Program Files\Java\jre1.8.0_60\lib\ext>cat TestClass.java
class TestClass
{
        public void displayMessage()
        {
                System.out.println("Hello World of Trials");
        }
}

C:\Program Files\Java\jre1.8.0_60\lib\ext>javac TestClass.java

C:\Program Files\Java\jre1.8.0_60\lib\ext>ls -lrt TestClass.*
-rwxrwxr--+ 1 Madhuri        None 103 Jun 17 12:23 TestClass.java
-rwxrwx---+ 1 Administrators None 418 Jun 17 12:25 TestClass.class

C:\Program Files\Java\jre1.8.0_60\lib\ext>



C:\Users\Madhuri\Documents>cat TestArt.java
class TestArt
{
        public static void main(String args[])
        {
                TestClass tc = new TestClass();
                tc.displayMessage();
        }
}

C:\Users\Madhuri\Documents>javac TestArt.java
TestArt.java:5: error: cannot find symbol
                TestClass tc = new TestClass();
                ^
  symbol:   class TestClass
  location: class TestArt
TestArt.java:5: error: cannot find symbol
                TestClass tc = new TestClass();
                                   ^
  symbol:   class TestClass
  location: class TestArt
2 errors

C:\Users\Madhuri\Documents>

Flow of class loading for a simple program

From the above answer, i thought it would be possible. I tried compiling with verbose option and understood it only accepts JAR files.

C:\Users\Madhuri\Documents>javac -verbose TestArt.java
[parsing started RegularFileObject[TestArt.java]]
[parsing completed 37ms]
[search path for source files: .]
[search path for class files: C:\Program Files\Java\jdk1.8.0_60\jre\lib\resources.jar,C:\Program Files\Java\jdk1.8.0_60\jre\lib\rt.jar,C:\Program Files\Java\jdk1.8.0_60\jre\lib\sunrsasign.jar,C:\Program Files\Java\jdk1.8.0_60
jre\lib\jsse.jar,C:\Program Files\Java\jdk1.8.0_60\jre\lib\jce.jar,C:\Program Files\Java\jdk1.8.0_60\jre\lib\charsets.jar,C:\Program Files\Java\jdk1.8.0_60\jre\lib\jfr.jar,C:\Program Files\Java\jdk1.8.0_60\jre\classes,C:\Prog
am Files\Java\jdk1.8.0_60\jre\lib\ext\access-bridge-64.jar,C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\cldrdata.jar,C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\dnsns.jar,C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\jacce
s.jar,C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\jfxrt.jar,C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\localedata.jar,C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\nashorn.jar,C:\Program Files\Java\jdk1.8.0_60\jre\lib\ex
\sunec.jar,C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\sunjce_provider.jar,C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\sunmscapi.jar,C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\sunpkcs11.jar,C:\Program Files\Java\jdk1.8
0_60\jre\lib\ext\zipfs.jar,.]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_60\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Object.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_60\lib\ct.sym(META-INF/sym/rt.jar/java/lang/String.class)]]
[checking TestArt]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_60\lib\ct.sym(META-INF/sym/rt.jar/java/io/Serializable.class)]]
[loading ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_60\lib\ct.sym(META-INF/sym/rt.jar/java/lang/AutoCloseable.class)]]
TestArt.java:5: error: cannot find symbol
                TestClass tc = new TestClass();
                ^
  symbol:   class TestClass
  location: class TestArt
TestArt.java:5: error: cannot find symbol
                TestClass tc = new TestClass();
                                   ^
  symbol:   class TestClass
  location: class TestArt
[total 570ms]
2 errors

C:\Users\Madhuri\Documents>

Do i need to create a JAR file in ext dir to load it automatically or else is there any way to load a class file directly by the program ??

Community
  • 1
  • 1
Mallesh
  • 80
  • 9
  • 1
    This may be an X/Y Problem. Could you describe why you want to hide the import and make it seem like your class was belonging to the JRE? – Fildor Jun 17 '16 at 07:22
  • 2
    See: [The Extension Mechanism](https://docs.oracle.com/javase/tutorial/ext/). Jars put in `jre/lib/ext` are automatically added to the classpath. But this is not the "normal" way to add jars to the classpath, so I do not recommend using this mechanism. The problem with it is that those jars will be added to **all** Java programs running on your computer, and that's not a good idea. Instead, just make sure the jar you need is in the classpath when running your application. – Jesper Jun 17 '16 at 07:24
  • I want to create some default methods in java to simplify my tasks and i want them to available to all my programs. – Mallesh Jun 18 '16 at 02:44
  • @Jesper As you said, May be it is not a recommended mechanism. But i want to give it a try to my thought whether it is possible or not. – Mallesh Jun 18 '16 at 02:50

2 Answers2

0

It is not possible of accessing the class from a local Java file without importing it in java. Even if you copy that java file in JRE/lib/ext directory which you want to access in other class, it does not mean that required class would not get added to default class path.

san544
  • 72
  • 10
0

It's feasible (if you are working with your personal machine), but not recommended if that environment, specifically that JVM is used for more Java systems. In other words, it's a bad practice.

The moment you put a jar file or class in that folder, it will be loaded by any Java process using that JVM, which is a big source for potential conflicts, meaning classloading issues such as nosuchmethoderror, classnotfoundexception, etc.

The jre/lib/ext folder is only to extend the core functionality of the JVM, meaning it has nothing to do with your programs.

Instead, use CLASSPATH to reference your custom jar files or classes.

You can play with the classloading debug flag (-verbose:class) to know to know where your classes are loaded from.

Ref:

What are the core extensions?

https://docs.oracle.com/javase/8/docs/technotes/guides/extensions/

How to use CLASSPATH:

https://docs.oracle.com/javase/tutorial/essential/environment/paths.html

Mario.Cadiz
  • 178
  • 12