简体   繁体   English

有没有办法从另一个java程序中运行java程序?

[英]Is there a way to run a java program from within another java program?

Ok, so basically, I wrote a java program that creates new java classes within the same folder as my current program. 好吧,基本上,我编写了一个java程序,它在与当前程序相同的文件夹中创建新的java类。 So far, that part works absolutely fine since it's just basically creating new text files. 到目前为止,该部分工作得很好,因为它基本上只是创建新的文本文件。 What I would like to know though, is if there is a way to run those created classes within the program without terminating the running program that created them. 我想知道的是,是否有办法在程序中运行这些创建的类而不终止创建它们的运行程序。 So, basically, I want to write a program that creates, edits, and runs java programs. 所以,基本上,我想编写一个创建,编辑和运行java程序的程序。 Is there any method, function, API, that helps to do something like this? 是否有任何方法,功能,API,有助于做这样的事情?

Also, I'm using eclipse to do this. 另外,我正在使用eclipse来做到这一点。

What I would actually try to do in that case without using external APIs, is to run cmd commands from Java. 在不使用外部API的情况下,我实际上会尝试做的是从Java运行cmd命令。

In that case you can compile the created Java files and then run the .jar file for example, using terminal commands once more.. 在这种情况下,您可以编译创建的Java文件,然后再运行.jar文件,再次使用终端命令..

For cmd commands through Java you can refer to this link: Run cmd commands through java 对于通过Java的cmd命令,您可以参考以下链接: 通过java运行cmd命令

I am not completelly sure how much this will help in your case since it kinda relies a lot on what are you actually trying to achieve and how. 我不完全确定这对你的情况有多大帮助,因为它有点依赖你实际想要实现的目标和方式。 But another option available to you is to call the main method of another class. 但是另一个可用的选项是调用另一个类的main方法。
For example if you want to pass no arguements you can make a call like class2.main(new String[0]); 例如,如果你想不传递任何争论,你可以像class2.main(new String[0]);那样进行调用class2.main(new String[0]);

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

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