简体   繁体   English

如何在用户创建的 jar 文件中导入 class?

[英]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.我有一个 jar 文件,我想将 class 从 Myjar.jar 导入到我的 Myclass.Z93F7426F44423D21C8 But I'm getting the error as error: package Display does not exist.但我收到错误错误:package 显示不存在。

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). 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.我有一个 Myclass.java 程序,我想从 jar 导入 Display.class。

Jar file contents:Myjar.jar Jar 文件内容:Myjar.jar

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

Display.java:显示.java:

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

Myclass.java: 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编译:javac -classpath ".:Myjar.jar" Myclass.java

Myclass.java:1: error: package Display does not exist Myclass.java:1:错误:package 显示不存在

You can not use the Jar file name.不能使用 Jar 文件名。 Use class name which your about to import or use.使用您将要导入或使用的 class 名称。

import Display;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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