cmd control command recognition in java - java

I am just wondering if there is a way, after launching "control wuaucpl.cpl" from my java script, to recognize if it has ran, or if it was blocked via active directory, or security settings. I have tried launching it with error streams, but so far I haven't seen any way of actually telling if it has opened up and ran or not. With similar tests I would just run a tasklist and check there to see if the process was running, but of course it comes up under explorer.exe and hence, would be impossible to tell.
EDIT:
This can also apply for any cmd control commands. EG: "control desk.cpl" or "control appwiz.cpl"
Any help would be greatly appreciated !
Thanks.

Related

Launch program from batch file generated by Java

I made a way to make my program, written in Java, update itself. The final JAR is wrapped in a EXE file, through Launch4j tool.
You need to know this piece of code:
System.getProperty("java.class.path").replaceAll("\\;\\.$", "")
gives me the actual path of the EXE. I tested it and it seems to be always working. This is important for the problem.
Now, basically the program pings a webpage and reads a series of values, which one of them is the latest version of the program. If it's greater, the program notifies the user for the update. So, the program downloads the remote data (updated EXE file) and stores them in the current running EXE file, whose filename is obtained through the method explained above. It works, but here comes the problem.
I could simply launch the downloaded EXE file and System.exit the current one, but I cannot do this, because my program works with smart cards: if two or more programs use the same smart cards, the new one won't work (I don't know why, I even restart the provider each time, but this is another story). So I prevent users from starting multiple istances of the program.
(My customers are not so smart to manually open the program each time they need it, so I needed to override the close button to make it stay in traybar, and wake up everytime it is needed. I even make it starts when Windows boots up).
So I have to close the current instance of the program, and launch again.
How I do this? I write a batch file which will basically look like this:
#echo off
taskkill /f /pid <pid of the exe program>
ping 127.0.0.1 -n 3 (this is a way to wait. I will eventually lower the waiting)
C:\Users\Mark\Desktop\program.exe (string generated by the method above. It should launch the program)
exit
Once written to disk, I execute it through Java:
Runtime.getRuntime().exec("cmd /c start " + batchFile.toString());
"batchFile" variable is a File object.
The problem is that the new downloaded program is not launched. A console window appears, shows the result of "taskkill" and "ping" (I will eventually mute them), but the program does not start. If I launch the batch file manually, it does.
Why? I really don't understand this behaviour.
Do you have any advice?
Thanks in advance!
TL;DR version:
The batch file executed by my Java program does not start the exe file written in it. Why?
I have the feeling you are trying to overwrite an executable file (EXE) that is currently running. AFAIK Windows locks such files and thus your updates should never happen.
To resolve your problem: I would split your application in two.
One part ensures the other part has the latest version, then executes that latest version.
For Java, something like this has been developed many years ago as WebStart technology, was marked as deprecated for Java 9 and removed thereafter. Meanwhile there is the project https://openwebstart.com/ that you might want to check out.

How to move Java runtime console to different terminal window?

I executed a Java program from the command line in terminal app A. I want to move the console to terminal app B without having to exit and re-execute the program.
I can think of a few potential ways to solve this, ranging from:
A) In Java implement a new InputStream and OutputStream that somehow can be wired to a new process started in terminal app B.
...to
B) Find a way to put the main Java process in terminal app A in the "background" so that original process can be reopened in a terminal app B.
Ideally, I want to be able to "log in to" and "log out of" my Java process from any terminal on my computer. Has anything like this already been accomplished, and which approach would be best to make it myself? I am open to solutions that involve Java code, shell scripts, or both.
My specs:
OSX: 10.12.4
Usually running zsh on iTerm
If I was using Linux, the perfect solution would be reptyr, a command line tool that allows you to easily switch terminal windows.
On Mac, the best solution I have found is screen. It can also be used to switch terminal windows but must be invoked before running java in order to work and seems a lot more complex.

Run Jar File on USB with Portable JRE

I'm in a bit of a fix at the moment with an application I'm developing. Currently I am trying to get the program to run solely off of a USB flash drive. I want to achieve this because the computers at work don't have admin privileges to install Java. I have Java installed on my flash drive and I have my program on it as well. I would prefer to not have any CMD windows pop up because it looks really ugly. I would just like my GUI to appear but I'm running into the dumbest problem in the world. Relative paths. For some reason windows shortcuts will not allow relative paths and I can't find a way around it.
I have tried shortcuts, vbscript, and batch files. I really can't find a practical solution to this.
If anyone can offer any help, that'd be great, thanks!
EDIT: inb4 "Why do you need to do this? That's suspicious!, etc". I'm the IT guy and the program generates a nice little printer friendly table of system information in html.
I finally got it! Due to #wOxxOm's link, I was able to make a command and work my way through the restrictions. Like you can't run cmd.exe /c ./dir/program.exe because cmd doesn't like the path to the program starting with ./ for some reason. It accepts "./dir/program.exe, but vbscript doesn't like the extra quotes. My final solution involved a combination of commands strung together with the && operator.
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("cmd.exe /c cd /java/64-bit/bin/ && java -jar ../../../resources/System_Information.jar"), 0, True
Just for reference, ../../../ was faster to type than another cd command.
Anyway, thanks for the help guys!

VLC dying when called from inside Java

I'm having a problem that, at this moment, I don't even know how to investigate properly. Any recommendations on how I can get more information are welcome and appreciated.
My company sells a product with a WinXP PC at its core. One of the product's tasks is being able to start a video player on demand -- VLC, in this case. (To be specific, VLC 0.8.6d; it's several years out of date, but upgrading is problematic for a few reasons.) The application responsible for starting the player and performing many, many other tasks is written in Java.
I have a test rig sitting next to my desk. It used to work just fine. But for some reason, it now gives a "Send Error Report" window when the Java app tries to start VLC: "VLC media player has encountered a problem and needs to close...." You know the one.
Clearly, I've done something that buggered things up. Problem is, I know neither what it could be nor how I would go about fixing it.
Stuff I know:
It's not a code bug. I run the same software on my development desktop machine, and it doesn't have this issue.
It's not the VLC install, nor is it a malformed video file. When I capture the command used to start it from Java and manually enter that command from a "cmd" window, it works fine.
It's not that sneaky bastich bug where Java punishes you if you don't manually drain STDERR and STDOUT when making a system call. I've got that covered.
I'm not getting any error messages or output when it fails; it just fails and gives me that pop-up window.
I'm stumped. Recommendations for either what it could be or how I can figure out what it is are very welcome.
Well, I’m not familiar with java and VLC, but I would do the following things:
Check that you have identical java virtual machines in both of your desktops. Just in case…
Check the process’s environment variables. They depend on parent process. Maybe VLC uses some of them.
Try to debug crashing with native debugger like WinDbg. Perhaps the call stack will give you more ideas.
Good luck!
My suggestions:
Create a simple java app that just launches VLC
Use your app to launch a simple command line windows program
Use your app to launch a complex program
Check to see if there is a memory constraint issue. Is VLC getting too little memory to run?
This really sounds like a memory/environment issue.
A number of things I would try
Make Sure both test and development machines are identical in every respect, the operating syste(if possible installed from same OS Disk), same JVM version, same memory allocation to JVM (you know those -X-ms stuffs). My fear is not with Java/JVM per se, it is with windows.
Make sure you can lunch for example Notepad from a Java app, and then something like Windows Media Player or MS Word.
Try and launch other versions of VLC to see if it is a VLC version problem.
Finally try and wipe the test box and re-install it(with Windows, you can never tell, a fresh installation might just do it!!)

The jar doesn't work properly... debugging post-build?

I'm in the dark as to how to even attack this particular problem and I have very little information to work with. Please bear with me.
My current project (a trading application) runs fine from Netbeans 6.9, and the build actually runs fine.. at first.. with `java -jar "my project.jar". It is a Swing application and everything comes up nicely. There are no error message in the terminal window.
However, when I engage the trading program itself (setting some of the threads into a more active state by clicking a command button), nothing happens. No error messages in the terminal, and none of the usual messages in the application. Like I said, it works fine from within Netbeans. Also, I've written other programs before using the same trading API (Interactive Brokers) and Swing, and haven't had this problem.
How do I even begin troubleshooting this problem?
I am admittedly build-stupid, meaning I have no idea how builds work and therefore no idea how to check if it is building properly.
I've checked /dist/lib and verified all of the libraries are present.
Thanks in advance for any suggestions.
Like Sean says, first check the JAR file to see if everything is there. Maybe the build is missing a file.
If that doesn't help, you can debug the application remotely. Here's a howto for Netbeans.
This happens because your Application is throwing an exception in the background. Try to execute your app using cmd:
java -jar yourappname.jar
You will see the exception in the cmd console, fix the exception and violá.
Best regards

Categories