简体   繁体   中英

How to import a class in a jar file that is user created?

I have a jar file and I want to import the class from Myjar.jar to my Myclass.java. But I'm getting the error as error: package Display does not exist.

This is for a Linux machine where I have created a jar file Myjar.jar that has only one class file Display.class (with no main method, but has other user defined method). I have a Myclass.java program that i want to import the Display.class from the jar.

Jar file contents:Myjar.jar

META-INF/
META-INF/MANIFEST.MF
Display.class

Display.java:

public class Display{
        public void disp(){
                System.out.println("in disp");
        }
}

Myclass.java:

import Display;

public class Myclass{
        public static void main(String[] argv){
                Dispay j=new Display();
                j.disp();
                System.out.println("Main");
        }
}

compiled by: javac -classpath ".:Myjar.jar" Myclass.java

Myclass.java:1: error: package Display does not exist

You can not use the Jar file name. Use class name which your about to import or use.

import Display;

public class Myclass{
        public static void main(String[] argv){
                Display j=new Display();
                j.disp();
                System.out.println("Main");
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM