“.:./*”在类路径中的含义是什么?

huangapple go评论119阅读模式
英文:

What is the meaning of “.:./*” in classpath?

问题

".:./*" 是一个用于类路径(classpath)的字符串。在这个上下文中,它用于告诉Java虚拟机(JVM)在哪里查找要执行的Java类文件。这个字符串具体含义如下:

  • .:代表当前目录,表示JVM应该在当前目录中查找类文件。
  • :./*:这部分是一个通配符,表示JVM应该查找当前目录下的所有JAR文件和类文件。

因此,".:./*" 表示JVM应该在当前目录中查找类文件,并且还应该查找当前目录下的所有JAR文件,以构建类路径,以便可以执行 MyDBCreateSchema 这个Java类。

英文:

I have the below script to run MyDBCreateSchema/MyDBCreateSchema.class to initialize a database.

java –cp ".:./*" MyDBCreateSchema dbHost 1433 id password DBName

What is ".:./*"?

答案1

得分: 0

在冒号之前的第一个“.”表示当前目录,因此包括当前目录中的所有.class文件。在冒号后面的“./*”我认为没有什么意义,因为我认为Java无法展开“*”字符。如果可以的话,这可能意味着当前目录下所有的子目录也将包含在类路径中(也就是说,这些子目录中的所有.class文件将被包括在类路径中)。

无论如何,这个代码非常难读,更有意义的做法是在bash脚本中创建一个变量,手动或使用类似以下方式将各个目录追加到其中:

CLASSPATH="."
for DIRECTORY in $(ls /some/directory); do
    CLASSPATH="${CLASSPATH}:${DIRECTORY}"
done

java -cp "$CLASSPATH" ...

你明白我的意思...

英文:

Well, the first "." before ":" means the current directory, so all .class files in the current directory are included. The "./*" after the ":" I don't think means anything, as I don't think Java can expand the '*' character. If it does, this probably means all the sub-directories immediately under the current directory will also be included in the classpath (i.e. all .class files in these subdirectories will, supposedly, be included in the classpath).

Either way, this is very unreadable, it makes more sense to create a variable in a bash script and append the individual directories either manually or with something like:

CLASSPATH="."
for DIRECTORY in $(ls /some/directory); do
    CLASSPATH+="${CLASSPATH:+:}$DIRECTORY"
done

java -cp "$CLASSPATH" ...

You get the drift...

huangapple
  • 本文由 发表于 2020年7月22日 10:38:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63025962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定