简体   繁体   English

在 macOS (Mac OS X) 上打印 Java 类路径

[英]Print the Java Classpath on macOS (Mac OS X)

On the command-line, how can I see all the folders and files comprising my current Java classpath ?在命令行上,如何查看包含当前Java 类路径的所有文件夹和文件?

The Oracle Tutorial says to use echo $CLASSPATH on Unix-like systems. Oracle 教程说在类 Unix 系统上使用echo $CLASSPATH But on a Mac (El Capitan) with Java 8 that prints nothing.但是在装有 Java 8 的 Mac (El Capitan) 上,什么也不打印。 As I recall, by default the CLASSPATH environment variable is not used on macOS.我记得,默认情况下,macOS 上不使用CLASSPATH环境变量。

I saw some Questions such as this that show setting the classpath, but I just want to verify the classpath.我看到了一些问题,比如这个,显示设置类路径,但我只是想验证类路径。

Is there some easy way from the command-line to examine the current classpath?从命令行是否有一些简单的方法来检查当前的类路径?

Unless you explicitly set the CLASSPATH variable in either the current shell or in your login profile (eg ~/.bash_profile ), then there is no way to show what it is.除非您在当前 shell 或登录配置文件(例如~/.bash_profile )中明确设置CLASSPATH变量,否则无法显示它是什么。

If you don't have the CLASSPATH set, then the default value of the class path is ".", meaning that only the current directory is searched.如果您没有设置 CLASSPATH,则类路径的默认值为“.”,这意味着仅搜索当前目录。 Specifying either the CLASSPATH variable or the -cp command line switch overrides this value.指定 CLASSPATH 变量或 -cp 命令行开关会覆盖此值。

In Java Code, using below helps to differentiate easily: System.out.println(System.getProperty("java.class.path"));在 Java 代码中,使用以下有助于轻松区分: System.out.println(System.getProperty("java.class.path")); System.out.println(System.getProperty("sun.boot.class.path")); System.out.println(System.getProperty("sun.boot.class.path"));

I'm not sure if $CLASSPATH is available in OSX by default.我不确定默认情况下 $CLASSPATH 在 OSX 中是否可用。 But, $PATH might help you.但是, $PATH可能会帮助你。 This variable has the information about the directories that contain executable commands.此变量包含有关包含可执行命令的目录的信息。

In your java code do:在您的 Java 代码中执行以下操作:

System.out.println(System.class.path);

This lists all the classpaths to the jar files (java platform classes), the class path for your project, etc. The classpaths are separated by semicolons.这列出了 jar 文件(java 平台类)的所有类路径、项目的类路径等。类路径用分号分隔。 Michael Markidis is right about "If you don't have the CLASSPATH set, then the default value of the class path is ".", meaning that only the current directory is searched." Michael Markidis 关于“如果您没有设置 CLASSPATH,那么类路径的默认值是“.”是正确的,这意味着只搜索当前目录。 And you should find that in this list.你应该在这个列表中找到它。

When classes are stored in a directory (folder), such as c:\\java\\MyClasses\\utility\\myapp, then the class path entry points to the directory that contains the first element of the package name (in this case, C:\\java\\MyClasses, because the package name is utility.myapp).当类存储在目录(文件夹)中时,例如 c:\\java\\MyClasses\\utility\\myapp,则类路径条目指向包含包名称第一个元素的目录(在本例中为 C:\\ java\\MyClasses,因为包名是utility.myapp)。

So if you use packages, the ending directory of your applications class path should be your root package.所以如果你使用包,你的应用程序类路径的结束目录应该是你的根包。

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

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