从java程序执行JAR中的java类

发布于 2025-01-03 07:15:11 字数 401 浏览 1 评论 0原文

我的问题是:

我有一个 test.java 类,它是主类。 JAR 文件中还存在一个类(非主类)。现在我想从 JAR 外部的主类编译并执行 JAR 中的演示类。请解释原因。

public class test
{   
public static void main(String args[])
{
    demo d1 = new demo();
    demo d2 = new demo("message passed from main class");
}
} 

public class demo
{
demo()}
System.out.println("message in demo class");
}
demo(String str)
{
System.out.println(str);
}}

My Question is:

I have a test.java class which is the main class. There is one more class(non-main class) which is present in JAR file. Now i want to compile and execute demo class present in JAR from the main class present outside the JAR. Please explain why.

public class test
{   
public static void main(String args[])
{
    demo d1 = new demo();
    demo d2 = new demo("message passed from main class");
}
} 

public class demo
{
demo()}
System.out.println("message in demo class");
}
demo(String str)
{
System.out.println(str);
}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月静好 2025-01-10 07:15:11

看看这个:在 JAR 文件中打包程序

主类和 JAR 文件准备就绪,您只需在类路径上运行带有 JAR 文件的主类,Java 就会找到它。假设 demo 类位于 demo.jar 中,并且您的主类是 test (如您的示例所示):

java -cp demo.jar test

Have a look at this: Packaging Programs in JAR Files

Once you have both your main class and JAR file ready, you can simply run the main class with the JAR file on the classpath and Java will find it. Assuming the demo class is in demo.jar and your main class is test (as in your example):

java -cp demo.jar test
Smile简单爱 2025-01-10 07:15:11

我猜正确的答案是“使用 ant”,但是:

javac test.java
javac demo.java
jar cf test.jar test.class demo.class
jar ufe test.jar test

然后,

java -jar test.jar

如果您打算对此进行扩展,并且继续不使用 ant,您可能希望您的构建系统在出现错误时停止,并且会注意到“ javac' 出错时不会返回非零值。将其 stderr 通过管道传输到临时文件,然后根据 grep 该文件的非零返回退出。例如,来自无法使用 ant 的设备上构建脚本:

cd src
for F in R.java Maze.java EmptyActivity.java; do
  javac -d ../build/classes $P/$F 2> ~/tmp/javac.out
  echo -en "\033[1m"; cat ~/tmp/javac.out; echo -en "\033[0m"
  grep error ~/tmp/javac.out && exit
done
cd ..

I guess the proper answer us 'use ant', but:

javac test.java
javac demo.java
jar cf test.jar test.class demo.class
jar ufe test.jar test

and then,

java -jar test.jar

If you intend to expand on this, and continue not to use ant, you'll likely want your build system to stop on an error, and will notice that 'javac' doesn't return non-zero on error. Pipe its stderr to a temporary file and then quit depending on grep's non-zero return of that file. e.g., from an on-device build script that cannot use ant:

cd src
for F in R.java Maze.java EmptyActivity.java; do
  javac -d ../build/classes $P/$F 2> ~/tmp/javac.out
  echo -en "\033[1m"; cat ~/tmp/javac.out; echo -en "\033[0m"
  grep error ~/tmp/javac.out && exit
done
cd ..
恰似旧人归 2025-01-10 07:15:11

简而言之,如果您想在 java 中使用任何,那么该类必须存在于class-path
因为当您运行程序时,所需的所有总是在类路径中查找..
所以你必须设置你的 demo.jar
没有办法设置类路径,看看这个如何设置类路径

或者简单地你可以使用java参数-cp做另一个选项

java -cp /your jar path/demo.jar test

Simply if you want to use any class in java then that class must be present in class-path
because when you run your program then all class needed for that is always looking in class path..
So you have to set class-path of your demo.jar
There is no of ways to set class path take a look of this How set class path

or simply you can do another option use java parameter -cp

java -cp /your jar path/demo.jar test
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文