简体   繁体   English

我如何从另一个jar内的shell脚本中调用jar内的java类

[英]how can i call a java class that is inside a jar from within a shell script that is inside another jar

I have a web application and inside a jar i have a shell script. 我有一个Web应用程序,并且在一个jar中,我有一个shell脚本。 From that shell script i want to execute a java class that is inside another jar file. 我想从该外壳脚本执行另一个jar文件中的java类。 I can actually execute, in a prompt, the class like this 我实际上可以在提示符下执行这样的类

/usr/bin/java -jar bar.foo.a.jar bar.foo.my.class / usr / bin / java -jar bar.foo.a.jar bar.foo.my.class

but the problem is when a task executes the script from inside the jar. 但是问题是当任务从罐子内部执行脚本时。 Then the bar.foo.a.jar is not located because its not at the same level 然后找不到bar.foo.a.jar,因为它不在同一级别

i tried with 我尝试过

/usr/bin/java -jar ../../bar.foo.a.jar bar.foo.my.class / usr / bin / java -jar ../../bar.foo.a.jar bar.foo.my.class

but i doesn't work, any suggestion on how could i achieve this? 但是我不工作,关于如何实现这一点的任何建议? is there a better way to do something like this? 有没有更好的方法来做这样的事情?

It is not clear from your question what you have actually tried, but here are a couple of pointers. 从您的问题尚不清楚您实际尝试了什么,但是这里有一些提示。

  • You cannot execute a shell script that is inside a JAR file. 您无法执行JAR文件中的Shell脚本。 You have to extract it to a free-standing file in the file system before you can execute it. 您必须先将其解压缩到文件系统中的独立文件中,然后才能执行它。

  • You cannot use a JAR file that is nested inside another JAR file. 您不能使用嵌套在另一个JAR文件中的JAR文件。 (Or at least, you can't without creating a custom ClassLoader.) The nested JAR file needs to be extracted as a free-standing file. (或者至少不能创建一个自定义的ClassLoader。)嵌套的JAR文件需要作为独立文件提取。

  • If your problem is with relative paths, you need to pay careful attention to what the "current directory" is at the point when the path is resolved. 如果您的问题是相对路径,则需要特别注意解析路径时“当前目录”的含义。 As an experiment, try replacing the relative path with the absolute path of the JAR file. 作为实验,请尝试用JAR文件的绝对路径替换相对路径。

  • The -jar option is not designed to work that way. -jar选项不适用于这种方式。 Assuming that the JAR file really exists at the relative location you are using, you probably need to do this: 假设JAR文件确实存在于您使用的相对位置,则可能需要执行以下操作:

     /usr/bin/java -cp ../../bar.foo.a.jar bar.foo.MyClass 

    The -jar option tells the JVM to execute the JAR file as a command, using the Main-Class declared in the JAR file's manifest as the entry point. -jar选项告诉JVM使用JAR文件清单中声明的​​Main-Class作为入口点,以命令的形式执行JAR文件。 If you do this: 如果您这样做:

     /usr/bin/java -jar ../../bar.foo.a.jar bar.foo.MyClass 

    you will find that bar.foo.MyClass is passed to the declared main program as its first command-line argument. 您会发现bar.foo.MyClass作为第一个命令行参数传递给声明的主程序。


My advice would be to take a few minutes to carefully read the manual pages for the java command, the jar command, and the linked pages that describe how the class path works, etc. 我的建议是花一些时间仔细阅读java命令, jar命令以及描述类路径如何工作的链接页面的手册页。

-jar option is a convenient way to execute a class inside a jar file, aka having an executable jar. -jar选项是在jar文件(又称为可执行文件)中执行类的便捷方法。 But using -jar file will ignore user class paths. 但是使用-jar文件将忽略用户类路径。

It might be easier for you to define proper classpath variable and execute your command with 您可能更容易定义适当的classpath变量并使用以下命令执行命令

/usr/bin/java -cp $CLASSPATH bar.foo.my.class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM