用于在正确路径中运行 Java 类的通用脚本
我有一个 package/bin 和 package/lib 的目录结构,其中 package/lib 包含 jar 文件,而 package/bin 应该包含一些脚本。
我有兴趣在 package/bin 中编写基本上从 lib/ 运行 java 类的脚本。这些脚本应该可以从任何目录运行,不一定是package/、package/bin/或package/lib。
这意味着 package/bin 中的脚本(我们称之为“run.sh”)应该执行以下操作:
- 识别 run.sh 脚本运行的目录。
- 删除该目录,以便我们最终得到 package/
- 添加 bin/ 到它,
- 并使用这个推导的目录运行必要的 java 类。
遵循步骤 1-3 的最佳方法是什么?我希望它几乎与平台无关,这样至少它可以在所有 Unix 系统和 OSX 上运行。我想避免使用 perl (我可以轻松地用 perl 编写它),而是选择 shell 脚本。
谢谢。
I have a directory structure of package/bin and package/lib, where package/lib contains jar files and package/bin should contain some scripts.
I am interested in writing scripts in package/bin that basically run java classes from lib/. These scripts should be runnable from any directory, not necessarily package/, package/bin/ or package/lib.
This means that the script in package/bin, let's call it 'run.sh' should do something like:
- identify the directory from which run.sh script is running.
- strip off the directory so that we end up with package/
- add bin/ to it
- run the necessary java classes with this deduced directory.
What would be the best way to follow steps 1-3? I would like it to be almost platform-independent, so that at the very least it would work on all Unix systems and OSX. I want to avoid using perl (I could easily write this in perl), and instead opt for a shell script.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
沿着 BASH 线......
$0 为您提供正在运行的脚本的路径
dirname $0
为您提供脚本所在文件夹的路径Along the BASH lines....
$0 gives you the path to the script you are running
dirname $0
gives you the path to the folder your script is in