【问题标题】:How can I use the classes from a jar file in another files, different directory?如何在另一个文件、不同目录中使用 jar 文件中的类?
【发布时间】:2020-01-27 00:55:52
【问题描述】:

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

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

【问题讨论】:

    标签: java class jar package classpath


    【解决方案1】:

    javac 用于编译,编译时请参考带有“/”(Windows)或“\”(基于Unix)和.java 文件后缀的文件路径:

    • javac com/cristian/example/Main.java

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

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

    【讨论】:

      猜你喜欢
      • 2013-10-16
      • 2011-04-30
      • 2010-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      相关资源
      最近更新 更多