繁体   English   中英

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

[英]How can I use the classes from a jar file in another files, different directory?

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

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

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

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

    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)和.java 文件后缀引用文件路径:

  • javac com/cristian/example/Main.java

java用于执行已编译的 class。 你参考.class编译文件,使用“.” 没有后缀.class 文件:

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

暂无
暂无

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

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