简体   繁体   English

从命令行编译Java

[英]Compiling java from the command line

I am trying to compile an application from the command line 我正在尝试从命令行编译应用程序

The programme compiles and runs fine in eclipse, howvever when i goto the command line and use javac to compile my java file i get 23 errors, the majority of which are Cannot Find Symbol, with an arrow pointing to the . 但是,当我转到命令行并使用javac编译我的java文件时,该程序可以在eclipse中编译并运行良好,但是我遇到23个错误,其中大多数是“找不到符号”,箭头指向。 in a package name. 在包装名称中。

Does anyone have any ideas on what i need to do differently? 有人对我需要做些什么有任何想法吗?

Thanks 谢谢

Your classpath is not set up correctly. 您的类路径设置不正确。 Look at your Eclipse project in the .classpath file. 在.classpath文件中查看您的Eclipse项目。 In there you will find a lot of classpathentry elements. 在这里,您会发现很多classpathentry元素。 You will need to replicate this for your command line compilation. 您将需要复制此代码以进行命令行编译。

To do this manually you must first set your CLASSPATH environment variable to a list of directories (or jar files) containing class definitions. 要手动执行此操作,必须首先将CLASSPATH环境变量设置为包含类定义的目录(或jar文件)列表。

You can also use a build tool called ant to automate this for you. 您还可以使用一个名为ant的构建工具来自动执行此操作。

I advise against setting the classpath as an environment variable because it is too intrusive (all your java programs will see it). 我建议不要将类路径设置为环境变量,因为它太侵入了(所有Java程序都会看到它)。

A command line for compiling a Java app which dependes on Log4j might look like this: 依赖于Log4j的用于编译Java应用程序的命令行可能如下所示:

javac -cp C:\dev\mvn\repo\log4j\log4j\1.2.16\log4j-1.2.16.jar AppenderTester.java

If you have multiple classpath entries you need to separate them with a semicolon. 如果您有多个类路径条目,则需要用分号将它们分开。

For ease of use you could create a startup script. 为了易于使用,您可以创建启动脚本。 This can be a simple batch script or a more elaborate ant script (which requires installing ant). 这可以是简单的批处理脚本,也可以是更复杂的ant脚本(需要安装ant)。

This is only the tip of the iceberg known as 'classpath hell'. 这只是被称为“ classpath hell”的冰山一角。

EDIT: you can also take a look at the Eclipse feature 'export runnable JAR', which packs your application together with all its dependencies in a JAR file. 编辑:您还可以查看Eclipse功能“ export runnable JAR”,该功能将您的应用程序及其所有依赖项打包在一个JAR文件中。

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

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