无法在命令行上将 Netbeans 生成的 .java 文件编译成 .class

Posted

技术标签:

【中文标题】无法在命令行上将 Netbeans 生成的 .java 文件编译成 .class【英文标题】:Cant compile Netbeans generated .java files into .class on command line 【发布时间】:2012-04-02 12:15:23 【问题描述】:

我将我的 java 程序构建为 uni 分配的一部分,并且作为提交的一部分,它必须能够在 DOS 窗口中编译和运行(阅读 Windows 命令窗口)。该程序在 Netbeans 中运行良好,但是当我将所有 .java 文件复制到一个单独的文件夹并尝试编译每个文件时,我得到了大量的编译错误。当我在 Netbeans 中创建文件时,我将它们全部作为一个包的一部分,并让 Netbeans 在每个类的顶部添加一个“包 *Project_Name*”。其余的是标准的java,没什么太复杂的。这是我第一次使用 Netbeans(我使用的是 7.1.1 版),它让我发疯。我可以创建一个 .jar 文件,我可以从命令行运行它,但我需要能够证明源代码可以从命令行编译,并且生成的文件也可以从命令运行线。有没有其他人遇到过这个问题,如果有,您是如何克服的,或者有谁知道如何解决这个问题。

我感觉它与对其他文件的引用有关,因为大多数错误与创建的对象和从其他文件调用的方法有关。例如,我有一个 Manager.java 文件,其中包含 Manager 对象的构造函数和方法,然后在另一个文件(比如 Proprety.java)中,我有使用 new 关键字创建 Manager 对象的代码。

我当前使用的 CLASSPATH 是

CLASSPATH = .;C:\Program Files\java\jdk1.7.0_03\lib;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;

PATH变量设置如下

...;C:\Program Files\Java\jdk1.7.0_03\bin;C:\Program Files\Java\jre7\bin

就是这样。我使用的唯一库是 java.util.* 和 java.io.* 所有其他引用都是我在 Netbeans 中创建的类文件

任何帮助都会非常感激。

SET 命令的结果:

C:\>set
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\Gunit\AppData\Roaming
asl.log=Destination=file
CLASSPATH=.;C:\Program Files\java\jdk1.7.0_03\lib;C:\Program Files\Java\jre6\lib
\ext\QTJava.zip;C:\Estateman
CommonProgramFiles=C:\Program Files\Common Files
COMPUTERNAME=GUNIT-PC
ComSpec=C:\Windows\system32\cmd.exe
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=\Users\Gunit
LOCALAPPDATA=C:\Users\Gunit\AppData\Local
LOGONSERVER=\\GUNIT-PC
MOZ_PLUGIN_PATH=C:\Program Files\SumatraPDF\
NUMBER_OF_PROCESSORS=2
OS=Windows_NT
Path=C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Windows\syst
em32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v
1.0\;C:\Program Files\Common Files\Acronis\SnapAPI\;c:\Program Files\Microsoft S
QL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:
\Program Files\Windows Live\Shared;C:\Program Files\QuickTime\QTSystem\;C:\Progr
am Files\Common Files\Avid;Avid;C:\Program Files\doxygen\bin;C:\Program Files\Co
mmon Files\Adobe\AGL;C:\Program Files\Java\jdk1.7.0_03\bin;C:\Program Files\Java
\jre7\bin
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 15 Model 6 Stepping 5, GenuineIntel
PROCESSOR_LEVEL=15
PROCESSOR_REVISION=0605
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files
PROMPT=$P$G
PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
PUBLIC=C:\Users\Public
QTJAVA=C:\Program Files\Java\jre6\lib\ext\QTJava.zip
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\Windows
TEMP=C:\Users\Gunit\AppData\Local\Temp
TMP=C:\Users\Gunit\AppData\Local\Temp
USERDOMAIN=Gunit-PC
USERNAME=Gunit
USERPROFILE=C:\Users\Gunit
VS100COMNTOOLS=c:\Program Files\Microsoft Visual Studio 10.0\Common7\Tools\
windir=C:\Windows

我正在运行的命令:

C:\Estateman>javac Manager.java

输出:

Manager.java:33: error: cannot find symbol
        Property temp0 = new Rental(24,"Bogan Ave","Geelong",3040,"Bob Jones
","0420685765","Victoria","Tues 8am",280.50,"23/4/2012");
        ^
symbol:   class Property
location: class Manager
Manager.java:33: error: cannot find symbol
        Property temp0 = new Rental(24,"Bogan Ave","Geelong",3040,"Bob Jones
","0420685765","Victoria","Tues 8am",280.50,"23/4/2012");
                             ^
symbol:   class Rental
location: class Manager
Manager.java:34: error: cannot find symbol
        Property temp1 = new Selling(56,"Maritime Rd","Perth",6000,"Alan Gib
s","0400345964","Perth","Wed 3pm",350485.00);
        ^
symbol:   class Property
location: class Manager
Manager.java:34: error: cannot find symbol
        Property temp1 = new Selling(56,"Maritime Rd","Perth",6000,"Alan Gib
s","0400345964","Perth","Wed 3pm",350485.00);
                             ^
symbol:   class Selling
location: class Manager
Manager.java:35: error: cannot find symbol
        Property temp2 = new Selling(901,"Preston Rd","Murdoch",6505,"Alan G
ibs","0400345964","Perth","Fri 10am",890000.00);
        ^
symbol:   class Property
location: class Manager
Manager.java:35: error: cannot find symbol
        Property temp2 = new Selling(901,"Preston Rd","Murdoch",6505,"Alan G
ibs","0400345964","Perth","Fri 10am",890000.00);
                             ^
symbol:   class Selling
location: class Manager
Manager.java:36: error: cannot find symbol
        Property temp3 = new Rental(83,"Joke Way","Murdoch",6505,"Grace Kell
y","0123493436","Winthrop","Fri 1pm",475.00,"18/4/2012");
        ^
symbol:   class Property
location: class Manager
Manager.java:36: error: cannot find symbol
        Property temp3 = new Rental(83,"Joke Way","Murdoch",6505,"Grace Kell
y","0123493436","Winthrop","Fri 1pm",475.00,"18/4/2012");
                             ^
symbol:   class Rental
location: class Manager
Manager.java:37: error: cannot find symbol
        Property temp4 = new Selling(167,"Joke Way","Murdoch",6505,"Bob Jone
s","0420685765","Victoria","Sat 3pm","17/5/2012");
        ^
symbol:   class Property
location: class Manager
Manager.java:37: error: cannot find symbol
        Property temp4 = new Selling(167,"Joke Way","Murdoch",6505,"Bob Jone
s","0420685765","Victoria","Sat 3pm","17/5/2012");
                             ^
symbol:   class Selling
location: class Manager
Manager.java:38: error: cannot find symbol
        Property temp5 = new Selling(2,"Ashbury Lane","Success",6110,"Ian Fi
elder","0895484453","Atwell","Mon 11am",900000.00);
        ^
symbol:   class Property
location: class Manager
Manager.java:38: error: cannot find symbol
        Property temp5 = new Selling(2,"Ashbury Lane","Success",6110,"Ian Fi
elder","0895484453","Atwell","Mon 11am",900000.00);
                             ^
symbol:   class Selling
location: class Manager
Manager.java:54: error: cannot find symbol
        Utils u = new Utils();
        ^
symbol:   class Utils
location: class Manager
Manager.java:54: error: cannot find symbol
        Utils u = new Utils();
                      ^
symbol:   class Utils
location: class Manager
Manager.java:84: error: cannot find symbol
            Property temp = new Selling(num,name,sub,code,person,phone,offic
e,open,price);
            ^
symbol:   class Property
location: class Manager
Manager.java:84: error: cannot find symbol
            Property temp = new Selling(num,name,sub,code,person,phone,offic
e,open,price);
                                ^
symbol:   class Selling
location: class Manager
Manager.java:92: error: cannot find symbol
                Property temp = new Rental(num,name,sub,code,person,phone,of
fice,open,price,vacant);
                ^
symbol:   class Property
location: class Manager
Manager.java:92: error: cannot find symbol
                Property temp = new Rental(num,name,sub,code,person,phone,of
fice,open,price,vacant);
                                    ^
symbol:   class Rental
location: class Manager
Manager.java:156: error: cannot find symbol
            Property temp = (Property)(itr.next());
            ^
symbol:   class Property
location: class Manager
Manager.java:156: error: cannot find symbol
            Property temp = (Property)(itr.next());
                             ^
symbol:   class Property
location: class Manager
Manager.java:165: error: cannot find symbol
                    Rental rTemp = (Rental)(temp);
                    ^
symbol:   class Rental
location: class Manager
Manager.java:165: error: cannot find symbol
                    Rental rTemp = (Rental)(temp);
                                    ^
symbol:   class Rental
location: class Manager
Manager.java:173: error: cannot find symbol
                    Selling sTemp = (Selling)(temp);
                    ^
symbol:   class Selling
location: class Manager
Manager.java:173: error: cannot find symbol
                    Selling sTemp = (Selling)(temp);
                                     ^
symbol:   class Selling
location: class Manager
Manager.java:180: error: cannot find symbol
                    Selling sTemp = (Selling)(temp);
                    ^
symbol:   class Selling
location: class Manager
Manager.java:180: error: cannot find symbol
                    Selling sTemp = (Selling)(temp);
                                     ^
symbol:   class Selling
location: class Manager
Manager.java:198: error: cannot find symbol
        Utils u = new Utils();
        ^
symbol:   class Utils
location: class Manager
Manager.java:198: error: cannot find symbol
        Utils u = new Utils();
                      ^
symbol:   class Utils
location: class Manager
Manager.java:199: error: cannot find symbol
        Property tmp = new Property();
        ^
symbol:   class Property
location: class Manager
Manager.java:199: error: cannot find symbol
        Property tmp = new Property();
                           ^
symbol:   class Property
location: class Manager
Manager.java:212: error: cannot find symbol
            Property temp = (Property)(itr.next());
            ^
symbol:   class Property
location: class Manager
Manager.java:212: error: cannot find symbol
            Property temp = (Property)(itr.next());
                             ^
symbol:   class Property
location: class Manager
Manager.java:238: error: cannot find symbol
        Property temp = new Property();
        ^
symbol:   class Property
location: class Manager
Manager.java:238: error: cannot find symbol
        Property temp = new Property();
                            ^
symbol:   class Property
location: class Manager
Manager.java:239: error: cannot find symbol
        Utils u = new Utils();
        ^
symbol:   class Utils
location: class Manager
Manager.java:239: error: cannot find symbol
        Utils u = new Utils();
                      ^
symbol:   class Utils
location: class Manager
Manager.java:249: error: cannot find symbol
        ((Property)(properties.get(i))).address.setStreetNum(num);
          ^
symbol:   class Property
location: class Manager
Manager.java:250: error: cannot find symbol
        ((Property)(properties.get(i))).address.setStreetName(name);
          ^
symbol:   class Property
location: class Manager
Manager.java:251: error: cannot find symbol
        ((Property)(properties.get(i))).address.setSuburb(sub);
          ^
symbol:   class Property
location: class Manager
Manager.java:252: error: cannot find symbol
        ((Property)(properties.get(i))).address.setPostCode(code);
          ^
symbol:   class Property
location: class Manager
Manager.java:266: error: cannot find symbol
        Utils u = new Utils();
        ^
symbol:   class Utils
location: class Manager
Manager.java:266: error: cannot find symbol
        Utils u = new Utils();
                      ^
symbol:   class Utils
location: class Manager
Manager.java:273: error: cannot find symbol
        Property temp = new Property();
        ^
symbol:   class Property
location: class Manager
Manager.java:273: error: cannot find symbol
        Property temp = new Property();
                            ^
symbol:   class Property
location: class Manager
Manager.java:280: error: cannot find symbol
        ((Property)(properties.get(i))).contact.setContact(name,phone,office
);
          ^
symbol:   class Property
location: class Manager
Manager.java:296: error: cannot find symbol
            Property temp = (Property)(itr.next());
            ^
symbol:   class Property
location: class Manager
Manager.java:296: error: cannot find symbol
            Property temp = (Property)(itr.next());
                             ^
symbol:   class Property
location: class Manager
Manager.java:311: error: cannot find symbol
        Utils u = new Utils();
        ^
symbol:   class Utils
location: class Manager
Manager.java:311: error: cannot find symbol
        Utils u = new Utils();
                      ^
symbol:   class Utils
location: class Manager
Manager.java:317: error: cannot find symbol
        Property temp = new Property();
        ^
symbol:   class Property
location: class Manager
Manager.java:317: error: cannot find symbol
        Property temp = new Property();
                            ^
symbol:   class Property
location: class Manager
Manager.java:320: error: cannot find symbol
        ((Property)(properties.get(i))).viewing.setTime(time);
          ^
symbol:   class Property
location: class Manager
Manager.java:332: error: cannot find symbol
        Utils u = new Utils();
        ^
symbol:   class Utils
location: class Manager
Manager.java:332: error: cannot find symbol
        Utils u = new Utils();
                      ^
symbol:   class Utils
location: class Manager
Manager.java:341: error: cannot find symbol
            Property temp = (Property)(itr.next());
            ^
symbol:   class Property
location: class Manager
Manager.java:341: error: cannot find symbol
            Property temp = (Property)(itr.next());
                             ^
symbol:   class Property
location: class Manager
57 errors

C:\Estateman>

提前致谢

【问题讨论】:

您是否设置了合适的类路径?您并没有真正提供任何我们可以用来提供帮助的实际信息。 你应该包含一些关于使用的类的基本信息。您是否设置了正确的类路径?你使用图书馆吗? 我要编译的文件位于 C:\Estateman 我使用的库是 java.util 和 java.io。其余的都是我自己创建的类 如果你发布你正在运行的命令和你得到的输出可能会有所帮助。 【参考方案1】:

好吧,我想,这里的关键是:

我将它们全部作为一个包的一部分,并让 Netbeans 在每个类的顶部添加一个“包 *Project_Name*”。

你的编译命令:

javac Manager.java

假设你的包名是projectname。然后Netbeans创建了一个目录projectname,并把你所有的Java文件Manager.javaProperty.java等等都放在里面。

在 Java 中,包结构直接对应于文件系统中的目录结构。例如。如果您在包x.y.z 中有一个类A,那么类A 的源代码文件在文件x\y\z\A.java 中。

因此,当您复制所有 *.java 文件时,您必须确保也复制原始目录结构。在您的情况下,您可以将您的课程Manager.javaProperty.java、...复制到C:\test\projectname。然后在你的 DOS 框中 cd 到这个目录并编译你的类:

javac -cp .. Manager.java

-cp .. 命令行选项的含义是,你的包/目录结构的根在C:\test 上一级。然后在编译Manager.java 时,Java 编译器会找到对类Property 的引用,并假定类Property 与类Manager 在同一个包中。所以编译器正在寻找类projectname.Property。使用指向C:\ţest-cp 选项,编译器知道它应该查看目录C:\test\projectname

编译后你可以运行你的程序:

java -cp .. projectname.Manager

【讨论】:

以上是关于无法在命令行上将 Netbeans 生成的 .java 文件编译成 .class的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 JDK 10 安装 Netbeans

在命令行上将 XLS 转换为 CSV

如何在 Linux / macOS 上将 Sass 与 NetBeans 一起使用

如何在 Mac OSX 上将 Access 数据库与 Netbeans 连接?

在 Windows 上将 Python 添加到 PATH

从命令行运行 Netbeans maven 项目?