简体   繁体   English

目录名称中存在句点时使用Java.exe运行

[英]running with Java.exe when period exists in directory name

I am trying to run the java prog I've built, but I want to run it from a specific directory. 我正在尝试运行已构建的Java Prog,但我想从特定目录运行它。 When specifying the relative path to the class file I want to run, that path contains a directory with a period in it, and it seems to be tripping java up; 当指定我要运行的类文件的相对路径时,该路径包含一个带有句点的目录,这似乎使java崩溃了。 So for example I try to run: 因此,例如,我尝试运行:

java -classpath myPath/myPath-1.2.3/myLongPath myPath/myPath-1.2.3/myLongPath/myProg

Java errors out saying that it cannot find the class (NoClassDefFoundError); Java错误指出无法找到该类(NoClassDefFoundError); This makes sense because I see that java is looking in different directory than the one I specified; 这是有道理的,因为我看到java在与我指定的目录不同的目录中查找。 It is looking in: myPath/myPath-1/2/3/myLongPath instead of: myPath/myPath-1.2.3/myLongPath 它正在查找:myPath / myPath-1 / 2/3 / myLongPath而不是:myPath / myPath-1.2.3 / myLongPath

Try as I might, I cannot figure out how to specify to java.exe that the directory I want it to look in contains periods. 尽我所能,我无法弄清楚如何向java.exe指定要查找的目录包含句点。 I tried \\ escaping the periods, but that doesn't help. 我尝试\\转义句号,但这无济于事。 Anyone run into this problem before? 有人遇到过这个问题吗? btw, I am running on linux within gnome terminal. 顺便说一句,我在gnome终端中的linux上运行。 Thanks for any help. 谢谢你的帮助。

The final parameter in the call to java is the name of the class to run. 调用java中的最后一个参数是要运行的类的名称。 This is not a file name, but a class name. 这不是文件名,而是类名。 It includes the full package name (unless the class is in the default package), separated by dots (not slashes). 它包括完整的程序包名称(除非该类在默认程序包中),并用点号(而不是斜杠)分隔。 Neither the classname nor any package name can include dots. 类名或任何包名都不能包含点。 The folder that represent the path to the package must not be included in the directories included in the classpath (only the top directory for the class folder should be there). 表示包路径的文件夹不得包含在类路径中包含的目录中(仅应包含类文件夹的顶层目录)。

In your case, that seems to be just myProg , but to make sure, what is the class name (including package name) of the class with the main method? 在您的情况下,那似乎只是myProg ,但是要确保,使用main方法的类的类名(包括包名)是什么?

Example: 例:

If I have a class mypackage.mysubpackage.MainClass , and the class file is in /home/me/project/1.3/build/mypackage/mysubpackage/MainClass.class , then the command to run the class would be java -cp /home/me/project/1.3/build mypackage.mysubpackage.MainClass . 如果我有一个类mypackage.mysubpackage.MainClass ,并且该类文件在/home/me/project/1.3/build/mypackage/mysubpackage/MainClass.class ,则运行该类的命令将是java -cp /home/me/project/1.3/build mypackage.mysubpackage.MainClass

Java uses the period as the package component separator, so it simply cannot appear in class names. Java使用句点作为包组件分隔符,因此它根本不能出现在类名中。 Since class names are tied to directory structures, it cannot appear in directory names used in class paths either, and no amount of escaping will help you there. 由于类名与目录结构相关,因此它也不会出现在类路径中使用的目录名中,并且没有任何转义可以帮助您。 (It would have been better to use the directory separator itself as the package component separator, but they differ between operating systems, and Java wanted to be OS independent. This is one of the prices to be paid for that.) (最好将目录分隔符本身用作包组件分隔符,但它们在操作系统之间有所不同,并且Java希望独立于操作系统。这是为此付出的代价之一。)

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

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