gpt4 book ai didi

java - 如何在另一个文件、不同目录中使用 jar 文件中的类?

转载 作者:行者123 更新时间:2023-12-02 00:11:00 24 4
gpt4 key购买 nike

我是 Java 新手,我想使用我在外部主类中创建的 jar 文件中的类,但是当我尝试从终端编译时,它找不到这些类。我不知道是我写错了导入语句,还是我设置了错误的类路径。

jar文件完整地址:/home/cristian/Escritorio/The\complete\Java\developer\course/Sección\11/Packages/ThejarFile/example/game/myJarFile.jar

主类完整地址:/home/cristian/Escritorio/The\complete\Java\developer\course/Sección\11/Packages/com/cristian/example/Main.java

这是我想使用这些类的主类:

    package com.cristian.example;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;


import ThejarFile.example.game.Player;
import ThejarFile.example.game.Monster;
import ThejarFile.example.game.ISaveable;

public class Main {
....

我试过这个:

 ~/Escritorio/The complete Java developer course/Sección 11/Packages$ javac -cp .:ThejarFile/example/game/myJarFile.jar com/cristian/example/Main.java

还有这个:

~/Escritorio/The complete Java developer course/Sección 11/Packages$ javac -cp .:/home/cristian/Escritorio/The\ complete\ Java\ developer\ course/Sección\ 11/Packages/ThejarFile/example/game/myJarFile.jar com/cristian/example/Main.java

最佳答案

javac用于编译,编译时使用“/”(Windows)或“\”(Unix-Based)引用文件路径以及.java文件后缀:

  • javac com/cristian/example/Main.java

java 用于执行已编译的类。您可以使用“.”来引用 .class 编译文件。不带后缀.class文件:

  • java -cp .:ThejarFile/example/game/myJarFile.jar com.cristian.example.Main

关于java - 如何在另一个文件、不同目录中使用 jar 文件中的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58121989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com