Reputation: 625
Java has built-in libraries that a java application can call at run time.
I created a class called test
, and it has method doSomething
, how can I make this class as a built-in class such that a java application can import this class directly (not as an external class)?
For example, for Scanner
class we can import directly:
import java.util.Scanner;
I want my test class to work just like Scanner
Class:
import java.util.test;
(Something like this, not sure the exact syntax)
Upvotes: 0
Views: 200
Reputation: 1032
You talk about Java Packages System. You can create your own library and build it as jar file. But better solution just use build system, like Maven, Gradle, Ant. If you not using build system, then just create the following folder hierarchy:
/java/util/test/MyTestClass.java
/MyClass.java
And in MyClass.java you should use:
import java.util.test.*;
Or
import java.util.test.MyTestClass;
For using MyTestClass
in MyClass
And for more info, read here http://docs.oracle.com/javase/tutorial/java/package/packages.html
Upvotes: 2
Reputation: 1377
I think you can create jar file of your classes, and put it under jdk/jre/lib/ext
folder.
To create a JAR file containing the Count.class file. Type the following in your command window:
jar cvf Count.jar Count.class
This creates a JAR file, Count.jar, and places the Count.class file inside it. Then I think you can import it like other java built-in classes. Hope It will help you.
Upvotes: 0
Reputation: 5647
You can't make your class be part of the standard Java library, because it is produced by Oracle and is the standard library, which means it is the same on all computers on the same JDK and JRE version.
If you want to have to import your class, then just put it in a package.
Directory structure:
myclasses
|---> Test.java
OtherClass.java
In OtherClass.java
import myclasses.Test;
public class OtherClass { ... }
In Test.java
package myclasses;
public class Test { ... }
Packages are a good way of organising java files. Note that the file in a package can also be an interface or enum. See the docs: https://docs.oracle.com/javase/tutorial/java/package/packages.html
Upvotes: 3