简体   繁体   English

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

[英]Cant compile Netbeans generated .java files into .class on command line

I built my java program as part of a uni assignment and as part of the submission it has to be able to be compiled and run in a DOS window (read Windows command window). 我将Java程序构建为uni分配的一部分,并将其作为提交的一部分,必须能够在DOS窗口(阅读Windows命令窗口)中进行编译和运行。 The program runs fine in Netbeans but when I copy over all the .java files to a separate folder and try and compile each one I get a huge number of compilation errors. 该程序在Netbeans中运行良好,但是当我将所有.java文件复制到一个单独的文件夹并尝试编译每个文件时,我遇到了大量的编译错误。 When I created the files in Netbeans, I made them all part of the one package and let Netbeans add a "package *Project_Name*" to the top of each class. 当我在Netbeans中创建文件时,我将它们全部放在一个包中,然后让Netbeans在每个类的顶部添加一个“ package * Project_Name *”。 The rest is standard java, nothing too complex. 其余的是标准的Java,没有什么太复杂的。 ITs my first time using Netbeans (version 7.1.1 is what Im using) and its driving me crazy. 我第一次使用Netbeans(Im使用的是7.1.1版),这使我发疯。 I can create a .jar file and I can run that from the command line but I need to be able to show that the source code can be compiled from the command line, and the resulting file(s) can also be run form the command line. 我可以创建一个.jar文件,并且可以从命令行运行该文件,但我需要能够证明可以从命令行编译源代码,并且还可以从该命令运行生成的文件线。 Has anyone else had this problem, and if so, how did you overcome it or does anyone know what to do to sort this problem out. 还有其他人遇到过这个问题吗?如果是,您是如何克服它的?或者没有人知道该如何解决这个问题。

I have a feeling its to do with references to other files as most of the errors are related to objects that are created and methods that are invoked from other files. 我感觉这与对其他文件的引用有关,因为大多数错误与创建的对象和从其他文件调用的方法有关。 fOr exmaple, I have a Manager.java file that has constructors and methods for a Manager object and then in another file (say Proprety.java) I have code to create a Manager object using the new keyword. 例如,我有一个Manager.java文件,其中包含Manager对象的构造函数和方法,然后在另一个文件(例如Proprety.java)中,我有使用new关键字创建Manager对象的代码。

The current CLASSPATH Im using is 我当前使用的CLASSPATH是

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

The PATH variable is set up like PATH变量的设置类似于

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

And thats it. 就是这样。 The only libraries Im using are java.util.* and java.io.* All other references are to the class files I created in Netbeans 我唯一使用的库是java.util。*和java.io. *。所有其他引用都指向我在Netbeans中创建的类文件。

Any help would really be appreicated. 任何帮助将不胜感激。

Results of SET command: 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

Command Im running: 命令我正在运行:

C:\Estateman>javac Manager.java

Output: 输出:

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>

Thanks in advance 提前致谢

Ok, I think, the key here is: 好的,我认为这里的关键是:

I made them all part of the one package and let Netbeans add a "package *Project_Name*" to the top of each class. 我将它们全部都放在一个包中,然后让Netbeans在每个类的顶部添加一个“包* Project_Name *”。

And your command to compile: 和您的命令进行编译:

javac Manager.java

Assuming your package name is projectname . 假设您的软件包名称是projectname Then Netbeans created a directory projectname and placed all your Java files Manager.java , Property.java , and so on in it. 然后,NetBeans中创建一个目录projectname ,并放置在您的所有Java文件Manager.javaProperty.java ,等它。

In Java the package structure corresponds directly to the directory structure in your file system. 在Java中,包结构直接对应于文件系统中的目录结构。 Eg if you have a class A in package xyz then the source code file for class A is in file x\\y\\z\\A.java . 例如,如果你有一个班级A在包xyz则类的源代码文件, A是在文件x\\y\\z\\A.java

So when you copy all your *.java files you have to ensure to also copy the original directory structure. 因此,当您复制所有*.java文件时,必须确保也复制原始目录结构。 In your case you can copy your classes Manager.java , Property.java , ... to C:\\test\\projectname . 在您的情况下,您可以将Manager.javaProperty.java类复制到C:\\test\\projectname Then in your DOS box cd to this directory and compile your classes with: 然后在DOS框中,将cd转到该目录,并使用以下命令编译类:

javac -cp .. Manager.java

The meaning of the -cp .. command line option is, that the root of your package/directory structure is one level up at C:\\test . -cp ..命令行选项的含义是,包/目录结构的根目录在C:\\test一级。 Then while compiling Manager.java the Java compiler finds the reference to class Property and it assumes that class Property is in the same package as class Manager . 然后,在编译Manager.java ,Java编译器会找到对Property类的引用,并假定该Property类与Manager类在同一包中。 So the compiler is looking for class projectname.Property . 因此,编译器正在寻找类projectname.Property With the -cp option pointing to C:\\ţest , the compiler knows it should look into directory C:\\test\\projectname . 使用-cp选项指向C:\\ţest ,编译器知道它应该查看目录C:\\test\\projectname

After compiling you can run your program with: 编译后,您可以使用以下命令运行程序:

java -cp .. projectname.Manager

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

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