简体   繁体   English

无法使用完全限定名称从CMD启动Java文件

[英]Unable to launch java file from CMD using Fully Qualified Name

As per title, I am able to compile the class just fine but I am unable to run it (despite using fully qualified name) 按照标题,我可以很好地编译该类,但是我无法运行它(尽管使用完全限定的名称)

Boss@DESKTOP-F8O3V2Q /cygdrive/c/eclipse/workspace/MVN/3cx-driver/src
$ java com.software._3cx.main.PBXConnection
Error: Could not find or load main class com.software._3cx.main.PBXConnection

PBXConnection.java PBXConnection.java

package com.software._3cx.main;

public class PBXConnection extends Proxy {

public PBXConnection(ServerSettings settings) {
    super(settings);
}

public static void main(String args[]) {
....

Would this be because of the package name containing _ underscore? 这是因为软件包名称包含_下划线吗?

Tree output 树输出

C:\eclipse\workspace\MVN\3cx-driver\src>tree
Folder PATH listing for volume OS
Volume serial number is 00000074 D019:C44D
C:.
└───com
    └───software
        └───_3cx
            └───main

DIR: 目录:

C:\eclipse\workspace\MVN\3cx-driver\src\com\software\_3cx\main>dir
 Volume in drive C is OS
 Volume Serial Number is D019-C44D

 Directory of C:\eclipse\workspace\MVN\3cx-driver\src\com\software\_3cx\main

06/04/2017  16:31    <DIR>          .
06/04/2017  16:31    <DIR>          ..
06/04/2017  16:22               115 EventProcessor.java
06/04/2017  16:31             1,434 PBXConnection.class
06/04/2017  16:21             1,994 PBXConnection.java
               3 File(s)          3,543 bytes
               2 Dir(s)  355,750,649,856 bytes free

Edit: 编辑:

Boss@DESKTOP-F8O3V2Q /cygdrive/c/eclipse/workspace/MVN/3cx-driver/src
$ javac -cp C:/Apache/apache-tomcat-7.0.56/lib/orderlycalls.jar com/software/_3cx/main/PBXConnection.java

Boss@DESKTOP-F8O3V2Q /cygdrive/c/eclipse/workspace/MVN/3cx-driver/src
$ java -cp C:/Apache/apache-tomcat-7.0.56/lib/orderlycalls.jar com.software._3cx.main.PBXConnection
Error: Could not find or load main class com.software._3cx.main.PBXConnection

Edit: Its actually Java 7 i am using (i though java 8 was running) but its not 编辑:我实际上正在使用它的Java 7(虽然Java 8正在运行),但不是

Boss@DESKTOP-F8O3V2Q ~
$ java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

Boss@DESKTOP-F8O3V2Q ~
$ javac -version
javac 1.7.0_79

Boss@DESKTOP-F8O3V2Q ~
$ which java
/cygdrive/c/Program Files/Java/jdk1.7.0_79/bin/java

Boss@DESKTOP-F8O3V2Q ~
$ which javac
/cygdrive/c/Program Files/Java/jdk1.7.0_79/bin/javac

Usually on Linux-like systems you would set up your classpath as follows with a colon: 通常在类似Linux的系统上,您可以使用冒号如下设置类路径:

java -cp "somelibrary.jar:." MyMainClass
                         ^

If you however use a Windows java you will get the error you desribed, indicating that java is not able to find the class you specified. 但是,如果使用Windows Java,则会得到您所描述的错误,表明java无法找到您指定的类。

If you ensure that you have a semicolon in your classpath, it should run your main class just fine: 如果确保在类路径中有分号,则它应该可以很好地运行主类:

java -cp "somelibrary.jar;." MyMainClass
                         ^

Note that if you use a unix-flavored java, the opposite is the case. 请注意,如果使用unix风格的java,情况恰恰相反。 You then may need to use : instead of ; 然后,您可能需要使用:而不是; .

Note also: while javac doesn't require the . 另请注意:虽然javac不需要. to find the file you want to compile (as you gave it as parameter), java doesn't know where to find your PBXConnection.class -file. 要找到要编译的文件(如您作为参数给出的那样), java不知道在哪里可以找到PBXConnection.class -file。 You told java that the classpath to search for contains only that orderlycalls.jar . 您告诉java ,要搜索的类路径仅包含orderlycalls.jar You need to explicitly say where java can also find your actual main-class. 您需要明确说明java还能在哪里找到您的实际主类。 So the . 所以. is necessary when you want to run your program, but it isn't necessary in your specific case for the compiler. 当您要运行程序时,它是必需的,但是对于您的特定情况,对于编译器来说则不必要。

Try the following. 请尝试以下方法。 It will work: 它将起作用:

set CLASSPATH=%CLASSPATH%;C:\eclipse\workspace\MVN\3cx-driver\src
java com.software._3cx.main.PBXConnection

Execute javac as below, this would compile the class file and place it in a directory structure as per the package structure. 按如下所示执行javac,这将编译类文件,并根据包结构将其放置在目录结构中。

javac -d PBXConnection.java

Then execute it using the below command, 然后使用以下命令执行它

java -cp . com.software._3cx.main.PBXConnection

This sets the current directory as your class path and then java will be able to find the class file as per the directory structure created in the previous step. 这会将当前目录设置为您的类路径,然后java将能够根据上一步中创建的目录结构查找类文件。

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

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