简体   繁体   English

在命令行中使用javac进行编译时出现“package javax.inject不存在”错误

[英]“package javax.inject does not exist” error while compiling with javac in commandline

I'm making my first steps toward learning JSF. 我正在迈出学习JSF的第一步。 I found this interesting book called "Core JavaServer Faces Third Edition". 我发现这本有趣的书名为“Core JavaServer Faces Third Edition”。

Trying to compile the first example, you can download the source code from: http://horstmann.com/corejsf/ . 尝试编译第一个示例,您可以从http://horstmann.com/corejsf/下载源代码。 When I type the following on the command-line 当我在命令行上键入以下内容时

javac UserBean.java

I get errors: 我收到错误:

package javax.inject does not exist
package javax.enterprise.context doe not exist

I have downloaded Java EE, Ant and GlassFish. 我已经下载了Java EE,Ant和GlassFish。

Here is a snap of my command-line: 以下是我的命令行:

C:\JSF-Tutorial\corejsf3-examples\javaee\ch01\login\src\java\com\corejsf>javac UserBean.java
UserBean.java:4: error: package javax.inject does not exist
import javax.inject.Named;
                   ^
UserBean.java:6: error: package javax.enterprise.context does not exist
import javax.enterprise.context.SessionScoped;
                               ^
UserBean.java:9: error: cannot find symbol
@Named("user") // or @ManagedBean(name="user")
 ^
  symbol: class Named
UserBean.java:10: error: cannot find symbol
@SessionScoped
 ^
  symbol: class SessionScoped
4 errors

C:\JSF-Tutorial\corejsf3-examples\javaee\ch01\login\src\java\com\corejsf>

Been googling how to compile a Java EE application for the last week but without anything useful. 谷歌搜索如何编译上周的Java EE应用程序,但没有任何有用的东西。

Would someone help me with this please, I need to solve this so I can move forward in my task to learn JSF. 请有人帮我解决这个问题,我需要解决这个问题,以便我可以继续学习JSF。

SP: I want to learn to compile Java EE applications bare hands before moving to compiling my Java EE projects with NetBeans. SP:在学习使用NetBeans编译Java EE项目之前,我想学会简单地编译Java EE应用程序。 I prefer to learn to work with GlassFish first then maybe latter I'll consider Tomcat. 我更喜欢先学习使用GlassFish,然后我会考虑使用Tomcat。

One more question; 还有一个问题; what's the difference between using Java EE and the GlassFish server to deploy my applications? 使用Java EE和GlassFish服务器部署我的应用程序有什么区别?

You need to include the JAR file containing those classes in the compile time classpath. 您需要在编译时类路径中包含包含这些类的JAR文件。

In your particular case with the GlassFish server, that's the /glassfish/lib/javaee.jar . 在您使用GlassFish服务器的特定情况下,这是/glassfish/lib/javaee.jar You can specify the classpath as -cp (or -classpath ) argument of javac command. 您可以将类路径指定为javac命令的-cp (或-classpath )参数。 It is a semicolonseparated string of disk file system paths pointing to JAR files and/or class folders which should be included in the compile time classpath. 它是一个半分号的磁盘文件系统路径字符串,指向JAR文件和/或类文件夹,它们应包含在编译时类路径中。

javac -cp /path/to/glassfish/lib/javaee.jar UserBean.java

javac will then look in there once it encounters an unknown class which is referenced by import , so that it can among others verify if you used it the right way. 一旦遇到import引用的未知类, javac就会查看它,以便它可以验证您是否以正确的方式使用它。

This has technically nothing to do with Java EE. 这在技术上与Java EE无关。 This is just basic Java. 这只是基本的Java。 I'd suggest to learn that first before diving into Java EE. 我建议在深入了解Java EE之前先学习一下。

In case you're using an IDE, then it's just a matter of attaching the target server as "Targeted Runtime" to the project. 如果您使用的是IDE,那么只需将目标服务器作为“Targeted Runtime”附加到项目中即可。 The IDE will then automatically do all magic as to the build path (the compile time classpath). 然后,IDE将自动对构建路径(编译时类路径)执行所有操作。

在netbeans 7.3中,您可以右键单击项目查看器中的libraries文件夹,选择“添加库”选项,然后从列表中选择“Java EE 6 API库”。

In NetBeans IDE 8.0 it's slightly different than described above for version 7.3 在NetBeans IDE 8.0中,它与上面针对7.3版所描述的略有不同

  1. Right click on Libraries in Java EE your project 右键单击Java EE中的Libraries您的项目
  2. Select Import... 选择Import...
  3. Choose either Java EE 6 API Library or Java EE 7 API Library depending on the version you're using. 根据您使用的版本选择Java EE 6 API LibraryJava EE 7 API Library
  4. Click the Import Library button 单击“ Import Library按钮
  5. Select the library you just imported and press the Add Library button. 选择刚刚导入的库,然后按“ Add Library按钮。

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

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