1

I am calling a jar from a batch file which essentially opens up a GUI app in windows 10. I would like to open this java app in maximized mode from a batch file. I found a lot of answers but they maximize my .bat file but don't maximize my app. Here is how I am calling the app:

cd c:\myfolder
"C:\otherfolder\java\bin\java.exe" -jar myjar.jar

It opens up the app but I want to open the app in maximized mode. That is, the jarfile contains a GUI app and that is what I want to maximize.

Dheeraj Dixit
  • 73
  • 1
  • 11
  • Does this jar contain a GUI app? Is this what you want maximized, or do you want the Windows console window to be maximized. Update the body of the question, please. –  Aug 22 '17 at 17:56
  • Sorry about the confusion. Jar contains a GUI app and that is what I want to maximize. I do not want to maximize my batch file. – Dheeraj Dixit Aug 22 '17 at 18:03
  • In that case, it is the app itself that is requesting to be maximized. See: https://stackoverflow.com/q/479523/1531971 –  Aug 22 '17 at 18:06
  • Does it have to be done within the java code? I would like to strictly use batch file to maximize it. Please let me know. – Dheeraj Dixit Aug 22 '17 at 18:08

1 Answers1

2

A linked answer tells how to make a typical JFrame-based app maximize on start, but the question here is, can we signal to an app via java -jar that it is supposed to be maximized?

Short answer: not without recompiling the Java app.

Longer answer: either we pass environment from the batch runtime or a property via the JVM we are invoking such that an app knows it is being asked to maximize itself. That is, we provide logic that checks for environment or a runtime property and invoke the GUI startup in a specific manner. Alternatively, we have the app itself take options and simply pass that option to it when we want it maximized.

I could also be wrong, if something like the start command works as described in another answer.


Update: I can confirm that start /MAX java -jar ... will sort of, but not really, work. It will maximize the java.exe window that is launched, but the GUI components within that will not be maximized.

There may be some shell trick that you can use here but I think it's a bit of a fool's errand. Any shell hack will end up being brittle and release specific. At the end of the day the GUI component that is actually in charge of displaying itself has little or no concept of platform specific requests like these.

This is a fundamental difference between so-called "native" GUI apps and apps that run via a virtual machine.

  • Can you please give me some examples for the Longer answer? I would like to try some things before I go and make changes to the code. Making changes to the code at this stage can cause delay in my project. Thanks! – Dheeraj Dixit Aug 22 '17 at 18:32
  • `System.getProperty()` and/or `System.getenv()` will do what you need. Then you invoke the JFrame with the right options depending on what you find. –  Aug 22 '17 at 18:33
  • I saw the START cmd earlier but it maximizes my batch file and NOT my gui app. I am trying to avoid making changes to the code at all cost. Another option I was exploring, I can see my app in the taskmanager. it shows up under "Java(TM) Platform SE binary (32bit)" but no name though. I can right click on it and maximize and minimize. Is it possible to access the gui app in task manager via batch file? – Dheeraj Dixit Aug 22 '17 at 18:39
  • Remember that your Java app is _not_ being run by the OS. The command shell has invoked a Java process. It is this Java process that opens, loads, and runs the Java bytecode represented by the classes, properties, and resources in the jarfile. AFAIK, there is no option to java.exe that knows how to tell a GUI component to be maximized (indeed, it has no knowledge of the classes in the jarfile beyond finding the main entry point), and the command shell has no access to the running Java bytecode. –  Aug 22 '17 at 18:44
  • I am getting an error trying to run my batch file with start /MAX "C:\otherfolder\java\bin\java.exe" - jar myjar.jar error : The system cannot find the file jar. – Dheeraj Dixit Aug 22 '17 at 18:53
  • `-jar` is a flag, and should be provide as-is, with no space. –  Aug 22 '17 at 19:02