如何使用 .jar 库编译 Java 程序

2023-10-14Java开发问题
2

本文介绍了如何使用 .jar 库编译 Java 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我无法让 javac 识别外部 .jar 文件,我正在尝试扩展其类.我在同一个目录中有两个文件:TestConsole.java 和 acm.jar.我正在使用以下命令从同一目录编译:

I can't make javac recognize an external .jar file, whose classes I'm trying to extend. I have two files in the same directory: TestConsole.java and acm.jar. I'm compiling from the same directory using the following command:

javac -classpath .:acm.jar TestConsole.java

但似乎 javac 只是忽略了 acm.jar.它给了我错误:

But it seems like javac is just ignoring acm.jar. It gives me the error:

TestConsole.java:1: package acm does not exist
import acm.program;
          ^

当然,acm.program 是 acm.jar 中的一个包.acm.jar 中的所有类都已编译;我只想在我的类中使用它们,而不是编译它们.

Of course, acm.program is a package in acm.jar. All of the classes in acm.jar are already compiled; I just want to use them in my classes, not compile them.

我做错了什么?

我在 Mac 上运行它,acm.jar 的目录结构似乎是有效的:它包含一个 acm/program 目录,其中包含 ConsoleProgram.classTestConsole 扩展的唯一类.

I am running this on a Mac, and the directory structure of acm.jar appears to be valid: It contains an acm/program directory, which has ConsoleProgram.class, the only class that TestConsole extends.

javac -classpath ".:acm.jar" TestConsole.java 也不起作用.

推荐答案

检查清单:

  1. 您在 acm.jar 中的类显示为:

    acm/program/CLASSX.class

    acm/program/CLASSY.class

  1. your classes in acm.jar appear as:

    acm/program/CLASSX.class

    acm/program/CLASSY.class

当用 jar tf acm.jar 倒出时

when decanted with jar tf acm.jar

您正在像这样导入它们:

You're importing them like:

导入 acm.program.CLASSX ;

导入 acm.program.* ;

import acm.program.CLASSX ;

or

import acm.program.* ;

这篇关于如何使用 .jar 库编译 Java 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13