Right, my title probably made no sense. However I'll explain it now.
I have a program coded in C++ that I wish to allow a keycombo to be pressed to run the program. My program does not run all the time it only runs when clicked then closes when the operation is complete. It simply does it's function then closes itself. Now here is the question.
Would it be simpler to attempt to edit my C++ program to run all the time via a thread in a class that then called the other class to run that did the function then stopped and listened for keypress again.
Or
Create a Java program that runs all the time and listens for a keyEvent and upon doing so runs the other programs exe. Then bundle the exe and jar in to one exe that is installed and run so they have access to each other.
If I did it this way is it even possible as far as I know key events need to have focus to be able to be detected. Meaning that I would need to be running the program as my main window to detect it? Or is that not true.
I'd looked at the java route as my Java is better than my C++. Is this do-able and if so what approach would be the better one?
Hope this is explained well enough let me know otherwise.
Related
I have java program (with a gui) which is running on a host. On runtime a user can add some data records. The program just works with them.
Later the system maybe shuts down or the program is just closed by SIGTERM.
Unfortunately the porgram only seems to store the data records if it is closed manually by the user with the "X".
Now i have the problem that i maybe lose some data if the program is closed by SIGTERM. Unfortunately there is no contact email address to ask the author for the change.
I tried to decompile it with "jd gui" which seemed to work more or less. I thought then i just could add the signal handler and invoke the "window close" method.
But "jd gui" created code like this:
/* 110:269 */ if ((??? = Thread.currentThread().getContextClassLoader()) != null) {
/* 111:270 */ return ???;
And i also get many other errors if i try to compile the source.
I think this is because the jar file is obfuscated by some tool. Unfortunately the decompiled code doesn't work and i am not able to recompile the file with the JFrame.
Long story short:
Is it possible to write a "wrapper program" which just handles SIGTERM and then invokes the JFrame "close method" (or how it is called) of the main frame (which is the only JFrame)?
I just do not know how to invoke such a method of an external running java program.
Thank you very much.
Best regards
Kevin
Create your own class with a main method which installs a shutdown hook to save before closing. then invoke the original programs main method. add this to the jar and use this class to launch the program.
Try to deobfuscate the application. Have a look here (2009) - Tool to deobfuscate Java codes.
Maybe it is possible to resolve the source so that you are able to diggin deeper into
the source and compile it.
Otherwise it is quite complicated to hook into a Swing based ui event queue from outside a wrapper application.
Maybe you can decompile the main class and replace its main method with your own and
add a shutdownhook see: Useful example of a shutdown hook in Java? which calls the onClose (deobfuscated) method.
Any way, an interesting and challening question!
I am trying to make a program that will run infinitely until I press a button. This program will run in the background so there is no display open at all times.
while(!certainButtonIsPressed)
{
//Do Something
}
How do I make it so that while certainButtonIsPressed is valid while not making a KeyBoardListener class? Is this possible without a Key listener of some sort?
Thanks!
You ask:
I am trying to make a program that will run infinitely until I press a button. This program will run in the background so there is no display open at all times. while(!certainButtonIsPressed) { //Do Something } How do I make it so that while certainButtonIsPressed is valid while not making a KeyBoardListener class? Is this possible without a Key listener of some sort?
So basically what you're trying to do is to trap all the keypresses of platform from a background process, and this is something that core Java cannot do without use of platform-specific native code that you provide, either through some library that you've obtained, or by meshing your Java program with some key-trapping utility. So if your question is, can this be done via just Core Java? And the answer is: no.
If you are looking for non-Java platform specific solutions, then you will need to give further details. My recommendation though is not to use Java for a task that can be performed much more easily and fully with another language, perhaps a scripting language such as AutoIt, if this were for a Windows environment.
I've designed a program to run off flash drives. My idea was to put an Eject button on the program so you can easily 'safely remove' the drive. However on Mac, you can't unmount the drive whilst the application is still running unless you do a force unmount. My question is, should I be doing this?
Isn't a force unmount similar to just pulling the drive out? Is it safe to make this option easily available?
Also is there any alternative?
I take it your application is running from the disk itself, yes?
Rather than force-eject the drive, you should spawn a separate process. Basically you write a little helper program that waits for your main program to quit, then ejects the disk, and finally quits itself. When you spawn the helper process, you will have to do so "without waiting." The terminology may be different depending on the language you're using ("in a new thread" or "detaching a process") but the basic idea is that you must launch a helper program to take over from your main program in such a way that your main program can quit.
It's bad form to force eject - you can't be sure the drive is not in use. On a Mac, for example, you have Spotlight reading/writing to external disks at all sorts of times.
I have a program which uses the Robot class in Java to automate a bunch of keypresses and clicks. The problem I am encountering is not being able to set breakpoints on certain methods to debug because the focus would change when I skip to the next expression.
Is there a way I can bypass this by changing the default continue hotkey (F8) in NetBeans to a low-level keyboard hook that will check system wide?
The reason for this is because the macro I am designing runs too fast for me to see each action occurring. If I set breakpoints along the program, I must alt-tab to the IDE and continue to the next breakpoint, which, unfortunately, interferes with the macro.
If you need to interact with the system in a way that interferes with your program, you must separate your debugger from your program.
In other words, run the two on separate machines and do a remote debug from one machine to another.
The easiest way to do so if you don't have or want to use two machines, is to run your program in a virtual machine. A cheap solution is to use vmware player along with a Linux distribution supported by Netbeans.
Whenever i want to connect to internet,i double click a connection icon(i created it earlier where username and password(for broadband) are stored) and click connect.The icon is in the network places(Windows XP)
May i know how to launch this connection from java or any other language? (I am asking this because my Internet Service Provider doesn't charge anything between 2 AM and 8AM :-) )
Creating a system task to run the program. You shouldn't need Java to execute a program on windows.
To use Scheduled Tasks in XP: http://support.microsoft.com/kb/308569
This is going to be unnecessarily difficult with Java, I believe. You'd have to write some native code to do the job for you, at which point you may as well write your whole program in C# or C++ anyway.
But, since you asked for a Java approach, you might want to look at the Robot class. It lets you move the mouse to a specific location on the screen, click, and otherwise automate the manual actions that you are doing. It's a very fragile solution.
Alternatively, if you can figure out what command the network connection shortcut is invoking, you can directly invoke it from Java using Runtime.exec.
(I don't really see why Java is good for this task, though.)