I need help or guidance when running two java programs and trying to read an exit error.
This'll be a little hard to explain so please forgive me if I'm not really clear.
Let's say that the chain of executing is something like this:
bat1.bat is the start, it execute myPGM.jar
myPGM.jar calls a 2nd bat: bat2.bat
bat2.bat calls an external bat, let's call it: runProcess.bat
runProcess.bat calls an external .jar and when it finish, it does echo errorlevel=%errorlevel%.
The problem is that the second .jar is failing and:
If I run all the chain: bat1.bat -> myPGM.jar -> bat2.bat -> runProcess.bat -> external.jar -> echo...
Although the external jar is failing, it always prints 0!
If I pause the execution before the runProcess.bat call, open a new cmd window and call it from there.. it prints the error correctly!
I'm really stuck with this, and probably don't have the knowledge to see where the problem is :(
My guess is that the problem can be related to the two java programs running at the same time?.. maybe the second one is like in another instance of a jvm, and that's why the bat always print errorlevel=0?
Or maybe is how I'm calling the .bat? I'm missing a parameter or something like this?
Well, reallyy really thanks!
Why do you need that? Anyway if you set the any variable in one command line you can not access it from an other.
Related
I have a Java project in eclipse which is divided in two parts; two different main classes that run two different Threads basically.
One contains loading, initialization and debug-showing procedures that are quite slow. While, the other manipulates the initialized data. In order to retrieve the information in the second part, the first one "saves" all the references inside a static map which contains instances of classes.
Does exist a way to run the first part only once, and then compile and run the second part more times? I tried with just set two different console and pressing the run button in different times but the static field of the first class looks not existing when the second runs.
I am working now only in the second part, so I need to test and start it many times. I really appreciate an help to save a lot of time wasted in always initialize the same thing.
ps : Everything works fine if I run both parts together.
thanks in advance
Luca
thanks to the replay (Multithreader, Stephen C) I am trying to make the question more clear and to ask how to solve it since my solution does not look the best one.....
EDIT 1 : The "first part" initializes the program and then runs an easy GUI which is periodically update. So as long as it shows up we shouldn’t care about how to manage input and output from the user
EDIT 2 : the "second part" reads information from the previous and send back strings to the GUI for debug purposes.
EDIT 3 : I do not have specific constrains in the shape of the project, so I can change the structure if there are better solutions. As well as for the way to run it.
FARTHER QUESTION 1 : there is a possibility to compile only one part of the project in eclipse while it runs all together? I mean, if two threads are running, can I stop one, re-compile it and run it again in a way that it can see the instances created from the first thread which never stops? Basically I need to refer at the same static variable loaded in memory, if it exists.
FARTHER QUESTION 2 : or more luckily does exist a way to store and load in a file instances of Java classes avoiding to write from sketch a mapping mechanism from/into txt files?
It is not entirely clear what you are asking here, but I'm assuming that you are talking about running the "first part" and the "second part" in the same JVM ...
Yes, it is possible. But it is not straightforward.
Basically, you need to refactor your code so that there is some kind of "control box" that the user can interact with from the outside. For instance, this might just be a simple command loop that reads commands from standard input and runs them. (Alternatively you could turn your application into a "service" that accepts requests over a network socket, via RMI, via HTTP, etcetera.)
Then you wire things up so that there is a "command" to run the "second part" of your application in response to the user's request.
That's the basics. The other thing you want to do is to "compile and run the second part [many] times". That implies that you need to set up your "control box" so that it can load a fresh copy of the code for the "second part" after you have modified and recompiled it. To achieve this, you will need to create a new ClassLoader object (each time) and use that to load the classes that make up the "second part". This is possible, but a bit tricky. The problems you are going to need to address include:
Splitting the "first part" and the "second part" into separate JAR files (or directory trees). The "first part" needs to be self-contained ... no dependencies on classes in the "second part".
Make sure that there are no runtime references from the "first part" data structures to instances of objects / enums in the "second part".
If you don't get the above right, you are likely to experience "permgen" storage leaks and mysterious type cast errors.
All in all, there's a lot that needs to be done to make this work. Unless you already understand all of the technologies involved, I'm doubtful that it will save you time overall. A better idea might be to figure out how to speed up the initialization of the "first part"; e.g. by doing lazy initialization, or caching the data structures using some fast / light-weight persistence mechanism.
I think it is better to change your design unless there is a requirement for it to stay the same.
Although I do not have the requirements or what you are actually trying to accomplish, I suggest you the following design:
1. App_1 does the calculations and then writes the results into file
2. App_2 reads checks for the file, if NOT exists display error message; otherwise read the file and keep going...
I guess I found a tricky solution. It is dirty but it works natively in the eclipse debugger.
I am running in debugging mode a main method which create a thread that works as a caller. This runs the first part of the project and wait until the initialization is complete (note that the first part doesn't end here, it remains looping to show debugging information based on a static class which evolves with the second part of the program). Then it starts with an infinite loop where it just calls the second part that I want to test and change: here there are also a breakpoint.
Well, now I can coding in the second parts while the eclipse debugger is waiting in the breakpoint than save it and hit F8. The debugger resumes, the algorithms runs and then it stops again in the breakpoint. Just check if works, eventually change something then save and hit F8 again without wait to re-initialize the first part of the project.
Probably this method has to be restarted after a while but still, it better then restart every time :)
many thanks to all your help.
If somebody has more elegant way to do that they are welcome!!
Suppose I have java code like this (only as Example):
public void someMethod(){
int a = 3;
int b = 2; // <-- stay debug here
a = b + 2;
System.out.prinln(a);
}
It is possible to skip execution of line a = b + 2; and go immidiatly to System.out.prinln(a);?
P.S. I use Intellij Idea 12.
It's not possible with the debugger to not execute parts of the code.
It is however possible to execute extra code and change values on variables so if you need to exclude one row from execution during debug you will have to alter your code to prepare for that kind of debugging.
public void someMethod() {
int a = 3;
int b = 2;
boolean shouldRun = true;
if (shouldRun) {
a = b + 2;
}
System.out.prinln(a);
}
You would then set a break point that changes the value of shouldRun without stopping execution. It can be done like this.
Note that
Suspend isn't checked
Log evaluated expression is used to alter a variable when the break point is hit
IntelliJ recently published a blog post about a plugin called Jump to Line that can accomplish this, even though IntelliJ itself doesn't have this functionality. It really is surprising that IntelliJ still doesn't have this functionality, when Visual Studio has had it for so many years!
https://blog.jetbrains.com/idea/2020/08/jump-to-any-line-while-debugging/
It is possible to skip lines only if you use hotswapping, or put in other words, code reloading tool - add code changes/new code at runtime. Hotswapping is the functions of replacing components without shutting down the system. Hotswapping can also refer to the ability to alter the running code of a program without needing to interrupt its execution.
There are various hotswapping tools like: JRebel (https://zeroturnaround.com/software/jrebel/) or HotSwapAgent (http://www.hotswapagent.org/)
You avoid having to rebuild the entire application to reload code changes, this is a huge time savings. Instead of running your full build process, simply use the compiler built into your IDE and the hotSwap agent/tool will reload the code into the JVM.
In this case, it would not be actually skipping, but you can just comment/change the lines and reload it. This tools are quite awesome!!!! It greatly speeds up the development/debugging process.
You can't just skip 'line execution' when debugging. You can press F8 to step over.
An option is to skip the execution of the rest of the method. You can invoke Force Return and even set a return value.
As stated here by ntotomanov , you can use HotSwap for it.
but just adding that i use remote debug to debug my Docker based apps , so if i want this kind of functionality i just comment out the code , and go to Build -> Recompile class or Rebuild Project . then Intellij issues Build & Attempting to HotSwap the code ! in case you did small thing like comment out the code it most of the times always succeeds. happy debugging.
It is not possible to skip the EXECUTION of the lines in IntelliJ and even in Eclipse.
Only thing you can do is you can do step over (F8)-which will trace line by line without
going inside any function.
One more thing is Step out(Shift+F8)- which will go to next debug point directly by executing in between lines in that single step.
I am currently learning how to use JUnitCore to run some tests with my application, I think I have it all working fine as it seems to move through the debugger without any problems but I have yet to get my test to print anything to the console System.out.println(...)
Result results= JUnitCore.runClasses(test.class);
System.out.println(results.getRunCount());
When I add a breakpoint to the first line it hits it, but I have another breakpoint inside test.class that when I move to the next breakpoint it never hits, it also wont let me step into the JUnitCore.runClasses(test.class); call either... But it does return 1 for results.getRunCount()
Does anyone know if this is working for me and you are not able to hit breakpoints inside tests this way or is there a problem with JUnitCore.runClasses(test.class)?
I JUnitCore.runClasses are not supposed to print out any message, it will just run the tests.
You will need to add code that will handle any failure and therefore produces some print out.
I'm using a CreateProcess call within a C++ program to execute a JAR file that runs a Java Swing GUI application. All works fine with the exception that the Java app starts off minimized and I want it to start with the window displayed. Here's the relevant code snippet:
// Construct the command string to be used for the CreateProcess call,
//including a parameter string
sprintf(cmdStr, "javaw -jar \"AppDir\\App.jar\" %s", parmStr);
// Create and initialized startup-info structure for use with CreateProcess call
STARTUPINFO startInfo;
ZeroMemory(&startInfo, sizeof(startInfo));
startInfo.wShowWindow = SW_NORMAL;
startInfo.dwFlags = STARTF_USESHOWWINDOW;
startInfo.cb = sizeof(startInfo);
PROCESS_INFORMATION procInfo;
ZeroMemory(&procInfo, sizeof(procInfo));
if (!CreateProcess(NULL, cmdStr, NULL, NULL, FALSE, 0, NULL, NULL, &startInfo, &procInfo))
{
MessageBox( dialogOwner, "Create Process Error", "Application not instantiated", MB_OK);
}
According to the MSDN literature, setting the wShowWindow flag to SW_NORMAL and dwFlags to STARTF_USESHOWWINDOW ought to do the trick but some of the comments I've read in this and other forums imply that sometimes those flags are ignored (e.g. for console apps) so I was wondering if that was the case here. For the record, I've had this problem before then it went away on its own and now it's back after I made some code changes. But I wasn't setting any flags in the startupinfo structure before, so I was hoping to achieve some consistency in behaviour by doing so. Any tips or pointers would be appreciated...
Sheldon R.
Okay, I've been working on a solution to my problem and I'm finally ready to talk about it, since it appears to be working :) I call my Java app one of two ways: either by popping up a dialog box first to collect login credentials, or by calling the app directly using saved credentials. For the login-dialog case, I call CreateProcess with the parameter "javaw...", whereas the saved-credential case appears to need "java..." to avoid the app starting up minimized. Aside from the difference in the parameter string, everything else about the CreateProcess call is the same. I don't know why I would use "java" in one case and "javaw" in the other, but since it's working, I won't question it :) But, of course, if someone wants to enlighten on the subject, I'd be happy to learn more. Thanks to #Jim Garrison for the suggestion, even if it wasn't ultimately the solution to my issue...
Sheldon R.
This is an update to my previous answer: The reason I had to call my java applet two different ways (i.e. "java" or "javaw") depending on the context, had to do with a bug in the C++ application from which I was calling my applet. The reason I know this is because a few months after fixing my issue, a newer version of this application was released, and this version didn't have the underlying bug, which essentially caused a new bug in my applet due to the "java" command doing what you'd expect i.e. instantiating a console window in addition to the applet window, much to the surprise of my business users :). So for the new bug-free version of the C++ application, I call my applet using the "javaw" command regardless of whether or not a dialog box is instantiated first to enable the user to enter login credentials...
Sheldon
Lets say I have 2 individual java applications javaapp1 and javaapp2.
from javaapp1, I am executing a .bat file (which is responsible for starting javaapp2).
javaaap1 and javaapp2 are independent to eachother.
Suppose I am doing it with process.exec or processbuilder.
Now my question is:
What does exitCode means in this case if its not 0.Does it mean that something went wrong in executing batch file
or in the code of javaapp2? or both?
Is it possible to capture errors from javaapp2 in javaapp1?If yes: How? Since i am not calling classes of javaapp2 directly.
Is javaapp2 errors and output are to be handled by javaapp1?
The exitcode will be whatever the other Java application has returned on System#exit() call. If you're executing it through a bat file, you need to ensure that it passes it back correctly.
You can let it write to stdout or stderr, it will then by available by respectively Process#getInputStream() and Process#getErrorStream().
If it contains code to handle the results mentioned by 1) and 2) correctly, then yes.
Related articles:
When Runtime#exec() won't - discusses the important traps to know.