在一致的环境中执行 Java 程序 [英] Execute Java programs in a consistent environment

查看:21
本文介绍了在一致的环境中执行 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 项目中配置的用于编译代码库的 jar.我正在开发一个个人项目,并希望类似地能够从命令行执行任意类,并试图确定如何最好地提供一致的 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 的机器上运行它们.特别是,我还希望能够限制可以执行的类/jar 的范围.例如,javacorp.sh 允许我们在 src/ 目录中运行任何东西,并且只有 javacorpunit.sh 包含 tests 中的类类路径中的/unit/ 目录.

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 命令,并且使用最少的样板?
  • 有什么办法可以挂钩 Eclipse 创建的 .classpath 文件?这并不能完全解决我的问题(例如一致的内存设置),但最好使用已经存在的数据.
  • 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 脚本? task 可以创建类路径来自 .

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

您可以为您想要的每种 shell 脚本创建一个 Ant 目标.make 然后可以调用这些 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 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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