javac 无法识别外部库 [英] javac not recognizing external libraries

查看:29
本文介绍了javac 无法识别外部库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Eclipse 中有一个项目的工作版本.

I have a working version of my project in eclipse.

我将项目导出为可运行的 jar.提取(转换为 .zip 后)并尝试从命令提示符编译特定的 java 文件(这样做是因为我有一个项目要求,该特定文件中的输入参数可以由没有 Eclipse 的用户修改和重新编译/运行)

I exported the project as a runnable jar. Extracted (after converting to .zip)and tried to compile a particular java file from the command prompt (Doing it this way since I have a project requirement, where input parameter inside that particular file can be modified and recompiled/run by users who wont have Eclipse)

我使用了一些外部库(例如:json-simple、gson 等).它们在编译过程中没有被识别.但是如果我运行类文件(来自 Eclipse 编译版本),它会正确执行

I have used some external libraries( for Eg:json-simple,gson etc).They arent getting recognized , during compilation. But if I run the class file(from the Eclipse compiled version), it gets executed properly

a)尝试从根文件夹编译(使用包名)javac 包名.java文件名.javab) 并进入包内并直接编译.javac javaFileName.java

a)Tried to compile from root folder(using package name) javac packageName.javaFileName.java b) and went inside the package and compiled directly. javac javaFileName.java

a) 部分根本没有编译,说 classNotFound.b) 部分开始编译但抛出一个错误,其中没有外部库被识别.(获取 --> 错误:无法找到使用外部库代码/导入的地方的符号)

The a)part didnt compile at all saying classNotFound. The b)part started compiling but threw an error where none of the external libraries got recognized.(Getting --> error: cannot find symbol for places wherever the code/import of the external lib is used)

推荐答案

a)尝试从根文件夹编译(使用包名)javacpackageName.javaFileName.java b) 然后进入包直接编译.javac javaFileName.java

a)Tried to compile from root folder(using package name) javac packageName.javaFileName.java b) and went inside the package and compiled directly. javac javaFileName.java

a) 部分根本没有编译,说 classNotFound.

The a)part didnt compile at all saying classNotFound.

是的.javac 要求您指定要编译的(第一个)源的文件系统路径.相反,您似乎将 .java 附加到所需的完全限定类名称的末尾.可能你想从解压后的 jar 的根目录编译,指定一个正确的路径:

Yes. javac requires you to specify a filesystem path to the (first) source(s) to compile. You appear instead to have tacked .java onto the end of the desired fully-qualified class name. Probably you want to compile from the root of the unpacked jar, specifying a correct path:

javac [options] package/name/className.java

对于类 package.name.className.(如果您指定适当的选项,您也可以从不同的工作目录进行编译,如下所述.)

for class package.name.className. (You can also compile from a different working directory if you specify an appropriate option, as discussed below.)

b)部分开始编译但抛出一个错误,其中没有任何外部库被识别.(获取 --> 错误:找不到符号使用外部库的代码/导入的地方)

The b)part started compiling but threw an error where none of the external libraries got recognized.(Getting --> error: cannot find symbol for places wherever the code/import of the external lib is used)

如果您正在编译的类依赖于其他也需要编译的类,那么 javac 可能会对它们提出类似的抱怨.从根编译(如 (a) 中),或通过 -sourcepath 选项指定到源根的路径.无论哪种方式,都没有理由进入源代码树进行编译.

If the class you're compiling depends on others that also need to be compiled then javac would likely make a similar complaint about them. Either compile from the root (as in (a)), or specify the path to the source root via the -sourcepath option. Either way, there's no reason to descend into the source tree to compile.

但外部库实际上是一个单独的,尽管相关的问题.您不需要编译这些,但您确实需要告诉 javac 将它们用作类的源.您可以通过 -classpath 选项来实现,您可以将其缩写为 -cp.如果它们被包装在罐子本身(即胖罐子")中,那么这应该相当容易,大致如下:

But the external libs are actually a separate, albeit related, question. You don't need to compile these, but you do need to tell javac to use them as sources of classes. You would do that via the -classpath option, which you can abbreviate to -cp. If those were packaged in the jar itself (i.e. a "fat jar") then that should be fairly easy, something along these lines:

javac -cp .:lib/dependency1.jar:lib/dependency2.jar package/name/className.java

lib"部分可能会有所不同,分隔符肯定会因操作系统而异(在 Windows 上是 ;,而在 Mac/Linux/Solaris 上是 :,如图所示).

The "lib" part may vary, and the separator definitely differs depending on OS (on Windows it is ;, whereas on Mac / Linux / Solaris is is :, as shown).

如果外部库打包到主 jar 中,那么过程是相同的,但您可能会遇到更大的挑战,找到所需的 jar.此外,如果您将此类 jar 移动到另一台机器,则它可能无法运行.不过,您可能应该查看 META_INF/MANIFEST.MF,因为它应该包含所需的信息.

If the external libs were not packaged into the main jar then the procedure is the same, but you might have a bigger challenge finding the needed jars. Also, such a jar is probably not runnable if you move it to a different machine. Nevertheless, you should probably look in META_INF/MANIFEST.MF, as it should contain the needed information.

这篇关于javac 无法识别外部库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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