本文介绍了在一致的环境中执行的Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪里工作,我们有一个shell脚本,让我们与所有必要的库和设置,像执行任意的Java类:

Where I work we have a shell script that allow us to execute arbitrary Java classes with all the necessary libraries and settings, something like:

#!/bin/sh
$JAVA_HOME/bin/java -cp LONG_LIST_OF_JARS -Xmx6g -XX:MaxPermSize=128m "$@"

而像这样用的:

javacorp.sh com.mycorp.SomeJob

这显然不是需要指定所有的Java 参数明确每一次更好,但我不喜欢,这只是手动连接到我们的Eclipse项目配置为编译罐子在codeBase的。我工作的一个个人项目,并希望同样能够在命令行中执行任意类,并试图确定如何最好地提供一致的的Java 环境。

This is obviously better than needing to specify all the java arguments explicitly every time, but I dislike that it's only manually connected to the jars configured in our Eclipse project for compiling the codebase. I'm working on a personal project and looking to similarly being able to execute arbitrary classes from the command line, and trying to identify how to best provide a consistent java environment.

目前我使用Eclipse来运行我的应用程序,但我希望能够通过命令行直接,或没有安装了Eclipse的机器上运行它们。尤其是,我还希望能够限制可以执行的类/罐范围。例如, javacorp.sh 让我们运行我们的的src / 目录什么,只有 javacorpunit.sh 包括在classpath中的测试/单位/ 目录中的类。

Currently I'm using Eclipse to run my applications, but I'd like to be able to run them from the command line directly, or on machines that don't have Eclipse installed. In particular, I'd also like to be able to limit the scope of classes/jars that can be executed. For example, javacorp.sh lets us run anything in our src/ directory, and only javacorpunit.sh includes classes in the tests/unit/ directory in the classpath.


  • 有没有使用Ant,Maven的,或其他一些构建工具在命令行中执行配置的的Java 命令,以最少的样板干净的方式是什么?

  • 有什么办法挂接到的.classpath 文件中的Eclipse创建?这并不完全解决我的问题(例如一致的内存设置),但它会是不错的使用已经存在的数据。

  • Is there a clean way to use Ant, Maven, or some other build tool to execute a configured java command at the command line, with minimal boilerplate?
  • Is there any way to hook into the .classpath file Eclipse creates? This doesn't wholly solve my problem (e.g. consistent memory settings) but it'd be nice to use data that already exists.

编辑:

我的措辞问题将是另一种方式什么是复制的命令行Eclipse的容易,运行当前文件的主要方法按钮,最彻底的方法?

Another way of phrasing my question would be "What's the cleanest way to replicate Eclipse's easy 'Run the current file's main method' button on the command line?"

推荐答案

你有没有考虑从生成Ant中shell脚本?该 < pathconvert> 任务可以从一个类路径<文件集的方式>

Have you considered generating the shell scripts from Ant? The <pathconvert> task can create a classpath from a <fileset>.

您可以为每种shell脚本,你想创建一个Ant目标。 制作则可以调用这些Ant目标来生成shell脚本。

You can create an Ant target for each kind of shell script you want. make can then call these Ant targets to generate the shell scripts.

这篇关于在一致的环境中执行的Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!