将Jar文件与-classpath JAVA结合使用 [英] Combining Jar file with -classpath JAVA

查看:146
本文介绍了将Jar文件与-classpath JAVA结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于编译一个类的问题,该类在Jar文件中有一些依赖类( MyJar.jar )。通过将目录树放在-classpath选项中(例如: javac -cp MyJar MyClass.java ),将检查所有子目录的类或仅检查顶级类目录树?
提前致谢。

I have a question regarding compiling a class which has some dependent classes in a Jar file (MyJar.jar). By putting a directory tree in a -classpath option (ex: javac -cp MyJar MyClass.java ), will all the subdirectories be checked for classes or only the top level classes in the directory tree? Thanks in advance.

推荐答案

将使用 -cp 被递归搜索:否

当类加载器进入类路径中指定的目录时,它使用包开始类位于子目录中。如果没有指定包,则类加载器将在目录的直接子项(类文件)下使用它。

when the classloader enters a directory specified in the classpath it starts using the package where the class is located as subdirectory. if no package is specified then the classloader will expect it under the immediate children (class files) of the directory.

它是 -cp的组合 direcoties / jars和包名

It's a combination of -cp direcoties/jars and package name.

假设您有以下目录结构

+ Project
    sayhello.jar
    + dir
        + sub
            + com
                + test
                    SayHelloMain.java

类的代码 SayHelloMain.java 是(注意包裹声明

package com.test;

import miscellaneous.so.SayHello;

public class SayHelloMain {
   public static void main(String[] args) {
       SayHello.sayIt();
   }
}

和jar文件 sayhello .jar 包含类 SayHello

这是你必须编译的方式如果命令行在与java源文件相同的目录中打开

this is how you'll have to compile the class SayHelloMain if the command line is opened in the same directory as the java source file

javac SayHelloMain.java -cp ..\..\..\..\sayhello.jar

或者如果在dierctory 项目中打开命令行

or if the command line is opened in the the dierctory Project

javac dir\sub\com\test\SayHelloMain.java -cp sayhello.jar

假设你已经在dierctory 项目中打开了一个命令行

Let's say u have opened a command line in the dierctory Project

这是如何运行类 SayHelloMain

java -classpath dir\sub;sayhello.jar com.test.SayHelloMain

班级名称必须完全合格因此 com.test.SayHelloMain

命令

java -classpath dir;sayhello.jar com.test.SayHelloMain

不会因为无法递归搜索目录 dir

will no work since the direcotry dir is not recursively searched

命令

java -classpath dir;sayhello.jar sub.com.test.SayHelloMain

也不起作用,因为没有这样的包 sub.com.test 。包只是在类

will also not work since there is no such package sub.com.test. A package is only that defined in the package declaration of a class

这篇关于将Jar文件与-classpath JAVA结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆