java 命令行 库,从命令行编译.java文件 - 外部库,类路径

本文档解决了一个开发者尝试从命令行编译包含外部库(如Joda-Time)的Eclipse Java项目的问题。通过理解并使用正确的命令行参数,如`-cp`和`-d`,可以指定类路径和输出目录。建议了解基本的构建过程,并考虑使用Maven或Ant等自动化构建工具以简化流程。
摘要由CSDN通过智能技术生成

I made a project in Eclipse and am now trying to ensure that the .java files compile from the command line. I've been trying to compile using javac *.java in the folder with all my .java files. However, this results in errors due to a reference to a class from an external library, Joda-time. I have the following .classpath file that Eclipse made for the project but don't know what to do with it.

I tried compiling with javac -classpath *.java but this only generates more errors. My source files are inside a package folder inside a 'src' folder. Where do I put my classpath file and my joda-time-2.2.jar file, and how do i get everything to compile?

[edit] I'm using Windows 7

解决方案

Judging by your .classpath file, this probably works:

javac -d bin -cp libs/joda-time-2.2.jar src/your/package/*.java

This assumes you run it in your project's directory.

The -d flag tells javac where to put the output files. The -cp flag is a shorthand for -classpath. I got these parameters based on the .classpath file in your question.

In general you can figure out what is needed by reading and understanding the errors in the output of javac. I bet you got many cannot find symbol errors at first, because the "symbols" were not on your classpath, so javac could not possibly know how to find them.

It is very important to understand how to build your projects, it is fundamental knowledge and a core competency of programmers.

Once you understand how to build manually like this, I recommend to look into build tools, preferably Maven, alternatively Ant that make building on the command line much easier. The majority of Java projects use these tools to build their products on the command line and in automated build systems, continuous integration, and so on.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值