Java, JAR in console - java

I need to start a Java application in the console by "double-clicking" the jar file. I found a workaround to achieve that:
Process p = Runtime.getRuntime()
.exec("cmd /c start cmd.exe /k \"java -jar CourseProjectServer.jar\"");
It runs pretty well, sure. But as you may guess it... Creates an infinite amount of itself until I stop it cause this is basically the first line of the application so when it starts it creates a new instance.
I came up with an idea like to start the process then grab it`s PID, save it in a file for example and then checking whether it is still running but found no way to do so.
Any suggestions? Is there a proper and not idiomatic way to achieve this?
P.S.: I know I can create a bat file to start the application but I don't want to do that. If that is what you want to write - skip it.
Any help appreciated.

Related

IntelliJ - Debugging process started by new process

I have a given jar file. It should be run with an argument which starts up another java process (my program).
I use a process builder to start a process (givenJar). Within this process, my program is started.
MyMain1: run processBuilder -> "java","-jar","givenJar.jar","java -cp myPath MyMain2"}
My problem is : How do I attach debugging? Is it possible?
I hope, that it makes sense... ;-)
Oh ok....
I solved it:
Edit configurations
Choose MyMain2
In "Defaults" go to "Remote"
Copy command line
Go back to Application/Main2
Insert command line in "Program arguments"

C# program wont be executed from Java

I have this C# program i made and while i can run it fine by clicking the exe file or by clicking on a batch file, I cant start up the program on a java program I made to run it. I have tried this line of code and couldn't get the software to run.
Runtime.getRuntime().exec("nameOfTheExeFile");
or set it to the batch file i made that starts the program.
Runtime.getRuntime().exec("nameOfTheBatchFile");
Now the interesting thing is when I try it with the batch file i get an error saying that the file cannot be found but when i double click the batch file it will start the exe file just fine.
I have even tried to use Process but I am not getting any luck with that process as well
List cmdAndArgs = Arrays.asList(new String[]{"cmd.exe", "/c", "ProgramName.exe"});
ProcessBuilder pb = new ProcessBuilder(cmdAndArgs);
Process p = pb.start();
Strange thing is i dont get any error at all. Even when i try unit testing i don't any error's at all. Is there a process I am missing or something ? I am lost on what to do.
Update:
When i check on the task manager i can see that the program is running but not the exe version. I see ProgramName.vshost.exe , is there a reason for this to be showing and not the exe file ?
Since your program is command line program you need to start it from cmd. I'm not sure if this is the best way to do it, but it works.
Runtime.getRuntime().exec("cmd /c start nameOfTheBatchFile");
Batch file:
start cmd.exe /k "nameOfExeFile"
exit

Java visible command line

I just tried to execute a java command using runtime like this:
Runtime.getRuntime().exec("shutdown -s");
to shutdown the computer, but I am training on a project that will get both my electronics and computer skills so I want to execute an avrdude command to program the mcu from Java and make a GUI program. So I want the cmd window to be visible when I run the command. I just made it visible by:
Runtime.getRuntime().exec("cmd.exe /c start");
but I cant write on the window I just created, any help pls????
thanks all
Following would do it for you.
CMD.EXE /K your_command
I got an idea but still need some help!
What about making a .bat file and running it using java File class
but still when I make a bat file and try to run I get the command multiple times like this:
http://i.imgur.com/cMddMh5.png
but all I did was only write avrdude in the bat file:
http://i.imgur.com/PDbi2vJ.png
still need help in that!!
thanks for all answers...
EDIT::
I just looked it up in the internet and figured out a solution which is making a .cmd file instead of a .bat file and that worked great without looping thanks for all the repliers..!! :)

Spawn process in Java, similar as double-click

I have a Java application and i would like to spawn a new process(start a .bat file), that will essintially do the same thing as by double-clicking on it.
I have tried both Runtime.getRuntime().exec() and ProcessBuilder in order to spawn that process. Both those approaches work (they can start the .bat file), but my problem is that they do not actually do the exact same thing as by double clicking on it.
More specifically, this .bat file starts up a JVM (java.exe MyMainClass) which is configured to run using Windows SxS (side by side). Thus, i have created appropriate java.exe.config and java.exe.manifest files. When i doulbe click on that, the java application starts and the appropriate .dlls are loaded succesfuly (reason i need SxS).
My problem is that when i start the exact same .bat file (with the exact same arguments and process environment), either by using ProcessBuilder or Runtime.getRuntime().exec(), it doesn't seem to take into consideration my SxS configuration, thus the .dlls that i need are not loaded at all, resulting in errors.
Does anyone have any clue how to launch this .bat file the same way as windows laucnhes it when i am double clicking on it?
Additionally, does anyone have any experience with Java SxS deployment? I cannot really understand why ProcessBuilder ignores my SxS configuration.
Thanks in advance.
You can try starting a cmd window, which loads your application bat file:
Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"C:\\path\\to\\the\\app.bat arg1 arg2\"");

Netbeans project still running when using `Runtime.getRuntime().exec("explorer.exe");`

I'm working on a (non-malicious) screen-locking sort of Swing application, and I've adapted code from Martijn Courteaux's answer at Use Java to lock a screen to do this. The problem is that when I use Runtime.getRuntime().exec("explorer.exe"); to reopen the Explorer process at program closing, Netbeans thinks that my project is still running because the resulting explorer.exe is running. CMD prompt and JCreator don't have this issue.
Can anyone give an example of the preferred way to call a command like explorer.exe to avoid this happening with Netbeans?
Edit: I close the Explorer process at the start of the program (which includes the taskbar). When I run Explorer, it's not to open a Windows Explorer window (which works totally fine with the given answers) but to restore the regular Windows UI.
The problem is Runtime#exec is waiting for the child process to exit. This is the default behavior.
If you want to execute a parentless process (a process in which the parent process can terminate even though the child is still running), you need to get a little more creative.
We use...
"cmd /C start /B /NORMAL " + yourCommand
I would highlight recommend using ProcessBuilder as it makes it significantly easier to build and execute external commands.
Something like...
ProcessBuilder pb = new ProcessBuilder("cmd", "/C", "start", "/B", "/NORMAL", "explorer.exe");
pb.start();
For example....
nb I run this is NetBeans and the program exited after/as explorer opened.
There is one little draw back. This doesn't like long file names. You'll need to find some way to produce short file names/paths for this to work. I was forced to use JNI solution for this
ProcessBuilder allows you to create and control processes.
Here's some example code, it's fairly complex but I don't have time to dumb it down (no offense): https://github.com/Xabster/Botster/blob/master/src/commands/ExecCommand.java

Categories