简体   繁体   English

Java使用jar中的类

[英]Java using classes from jar

This must be a super overasked question. 这肯定是一个过于挑剔的问题。 Although here goes: 尽管这里:

I have a java file for testing around (hworld.java) and am trying to import conio.jar, a JAR which is a wrapper of Conio. 我有一个用于(hworld.java)进行测试的Java文件,正在尝试导入conio.jar,这是一个Conio的包装JAR。 The JAR contains only one class file (conio.class) and META-INF. JAR仅包含一个类文件(conio.class)和META-INF。 Trying to do import conio.* or import conio.conio shows me this: 尝试执行import conio.*import conio.conio向我展示了这一点:

C:\Documents and Settings\Nick\Desktop>javac -cp *.jar; hworld.java
hworld.java:3: error: package conio does not exist
import conio.*;
^
1 error

And compiling it like javac -cp conio.jar hworld.java still errors out while compiling. 并且像javac -cp conio.jar hworld.java一样javac -cp conio.jar hworld.java仍然在编译时出错。 I even extracted the jar and had conio.class in the same directory as hworld.java but to no avail. 我什至提取了jar并将conio.class与hworld.java放在同一目录中,但无济于事。 The JAR is in the same directory as hworld.java, as well. 该JAR与hworld.java也在同一目录中。

Anyone have any idea on how to fix this? 有人对如何解决这个问题有任何想法吗?

It's actually not possible. 实际上是不可能的。 You need to put the other class in a package if you want to import it. 如果要导入其他类,则需要将其放在包中。

What's the syntax to import a class in a default package in Java? 在Java的默认包中导入类的语法是什么?

You don't mention whether conio.class is defined in package conio. 您没有提及conio.class是否在包conio中定义。 If it is not, then simply use the class without importing it. 如果不是,则只需使用该类而不导入它。 Remove the import. 删除导入。

Find out what package Conio is in - an easy way to do this is to open the jar as a zip file, the package will correspond with the folder structure of the archive. 找出Conio所在的软件包-一种简单的方法是将jar作为zip文件打开,该软件包将与档案的文件夹结构相对应。 For example if Conio is in x/y/z then import xyzConio and compile/run with conio.jar on the classmate. 例如,如果Conio在x / y / z中,则导入xyzConio并使用同班同学上的conio.jar进行编译/运行。

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

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