从命令行编译Java [英] Compiling java from the command line

查看:73
本文介绍了从命令行编译Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从命令行编译应用程序



该程序可以在Eclipse中编译并运行良好,但是当我进入命令行并使用javac进行编译时我的Java文件出现23个错误,其中大多数是找不到符号,箭头指向。



有人对我需要做的事情有不同的想法吗?



谢谢

解决方案

我建议不要将classpath设置为环境变量,因为它太侵入了(所有Java程序都会看到它)。



用于编译依赖Log4j的Java应用程序的命令行可能看起来像这样:

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

如果您有多个类路径条目,则需要使用分号将它们分开。



<为了易于使用,您可以创建一个启动脚本。这可以是简单的批处理脚本,也可以是更复杂的ant脚本(需要安装ant)。



这只是冰山一角,被称为类路径地狱。



编辑:您还可以查看Eclipse功能 export runnable JAR,该功能将您的应用程序及其所有依赖项打包到一个JAR文件中。


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 . in a package name.

Does anyone have any ideas on what i need to do differently?

Thanks

解决方案

I advise against setting the classpath as an environment variable because it is too intrusive (all your java programs will see it).

A command line for compiling a Java app which dependes on Log4j might look like this:

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).

This is only the tip of the iceberg known as '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.

这篇关于从命令行编译Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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