“包 javax.inject 不存在"在命令行中使用 javac 编译时出错 [英] "package javax.inject does not exist" error while compiling with javac in commandline

查看:128
本文介绍了“包 javax.inject 不存在"在命令行中使用 javac 编译时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在迈出学习 JSF 的第一步.我发现了一本有趣的书,叫做Core JavaServer Faces 第三版".

I'm making my first steps toward learning JSF. I found this interesting book called "Core JavaServer Faces Third Edition".

尝试编译第一个示例,您可以从以下位置下载源代码:http://horstmann.com/corejsf/.当我在命令行中输入以下内容时

Trying to compile the first example, you can download the source code from: http://horstmann.com/corejsf/. When I type the following on the command-line

javac UserBean.java

我收到错误:

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

我已经下载了 Java EE、Ant 和 GlassFish.

I have downloaded Java EE, Ant and GlassFish.

这是我的命令行快照:

C:JSF-Tutorialcorejsf3-examplesjavaeech01loginsrcjavacomcorejsf>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-Tutorialcorejsf3-examplesjavaeech01loginsrcjavacomcorejsf>

上周一直在搜索如何编译 Java EE 应用程序,但没有任何有用的东西.

Been googling how to compile a Java EE application for the last week but without anything useful.

有人能帮我解决这个问题吗,我需要解决这个问题,这样我才能继续学习 JSF.

Would someone help me with this please, I need to solve this so I can move forward in my task to learn JSF.

S.P:在开始使用 NetBeans 编译我的 Java EE 项目之前,我想学习如何直接编译 Java EE 应用程序.我更喜欢先学习使用 GlassFish,然后可能会考虑使用 Tomcat.

S.P: I want to learn to compile Java EE applications bare hands before moving to compiling my Java EE projects with NetBeans. I prefer to learn to work with GlassFish first then maybe latter I'll consider Tomcat.

还有一个问题;使用 Java EE 和 GlassFish 服务器部署我的应用程序有什么区别?

One more question; what's the difference between using Java EE and the GlassFish server to deploy my applications?

推荐答案

您需要在编译时类路径中包含包含这些类的 JAR 文件.

You need to include the JAR file containing those classes in the compile time classpath.

在您使用 GlassFish 服务器的特定情况下,就是 /glassfish/lib/javaee.jar.您可以将类路径指定为 javac 命令的 -cp(或 -classpath)参数.它是一个以分号分隔的磁盘文件系统路径字符串,指向应该包含在编译时类路径中的 JAR 文件和/或类文件夹.

In your particular case with the GlassFish server, that's the /glassfish/lib/javaee.jar. You can specify the classpath as -cp (or -classpath) argument of javac command. 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.

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

javac 会在遇到由 import 引用的未知类时查看那里,以便它可以验证您是否以正确的方式使用它.

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.

这在技术上与 Java EE 无关.这只是基本的Java.我建议在深入研究 Java EE 之前先学习这一点.

This has technically nothing to do with Java EE. This is just basic Java. I'd suggest to learn that first before diving into Java EE.

如果您使用的是 IDE,那么只需将目标服务器作为目标运行时"附加到项目中即可.然后,IDE 将自动对构建路径(编译时类路径)执行所有魔术.

In case you're using an IDE, then it's just a matter of attaching the target server as "Targeted Runtime" to the project. The IDE will then automatically do all magic as to the build path (the compile time classpath).

这篇关于“包 javax.inject 不存在"在命令行中使用 javac 编译时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆