Tools: Win 7, Launch4J 3.5, Simple Hello world Java console app (bundled in a JAR file)
Hello all,
I have a basic JAVA console application that doesn't request any inputs, just a simple application that opens a console window and displays Hello World text.
I built it so simple so I can experiment with Launch4J 3.5 and build an executable file from the jar file.
Everything looks fine, the exe builds successfully but when I launch it nothing happens, I get the hour glass for a few seconds then nothing. I check the Task Manager and there's no process stuck in there.
See my settings in Launch4J, I only filled in the basics, I tried with and without an entry in the Wrapper manifest field:
Output file: C:\Development\SFDC\ProjectX\out\exe\ProjectX.exe
Jar: C:\Development\SFDC\ProjectX\out\artifacts\ProjectX_jar\ProjectX.jar
Wrapper manifest: C:\Launch4j\manifest\uac.exe.manifest (also tried with leaving this blank)
The rest is all left as default.
If by launching you mean double clicking it, no - nothing you can see will happen; you have to 'tell' Java to run your application with an associated console. To do this, you may create a new .bat file: Simply open a text editor and insert the following line:
java -jar NAME.jar
where "NAME" is the name of your application. Save the text file in .bat format, not in .txt, and place it in the same directory as your application. You can launch your application by double-clicking that file.
The reason it does not pop up in your task manager is because probably (I cannot know) your application only prints out a simple message and does nothing more. In non-console mode, it will just call your print (println or any other console) method without having any visual effect as there is no console to print the message to. In both cases however, if you only print something and do not perform other operations that 'last', such as listening for input, your programm will terminate as it has reached the end of the main method.
Yes. You do need to use the console mode.
You also do need to have some method of keeping the console window open, because it closes the console the moment the program terminates. Use scan.nextLine(); or Thread.sleep(i) if you really need to.
Possibly you need to use launch4j in console mode, see this answer: lauch4j hello world program
Related
When I click on the downloaded libgdx file I select run with Java then the libgdx window pops up I check android (and tried to check desktop too) and leave the names default and some times I tried to change them and for extensions I leave it as default the only one checked is box 2D.
Its says in the little console below that it's generating an app in my desired path then its just stuck there for hours.
(I checked task manager and its not using almost any resources other than 60 mbs of ram)
This problem could possibly happen if the application cannot write into the destination folder you choose. So avoid making the destination folder inside the system32 folder or such.
Run the jar file from the command prompt to see the exception it generates which should explain the problem:
Hold down the Windows button and tap the 'r' key.
In the dialog type 'cmd' then enter.
Copy this text using ctrl-c, include all quotes. Adjust the file names to point to your Java VM runtime and your downloaded
gdx-setup.jar file: "C:\Program
Files\AdoptOpenJDK\jdk-8.0.222.10-hotspot\bin\java.exe" -jar "C:\Users\keith\Downloads\gdx-setup.jar"
Paste the text into the command prompt by pressing ctrl-insert (the zero key on the numpad). Alternatively, tap the top left icon,
then edit, then paste.
See what the specific exception is in the cmd console when the gdx setup program runs.
I created a program that is console based, but the problem I have is when (Through eclipse) I turn it into a runnable .jar file and run it, nothing happens. I believe this is because it is printing to the console but the .jar has no idea how it's supposed to show that. So my question is is there a way to redirect the console to a new window? I have seen similar questions but none of the methods seem to work. I cannot use the console because I plan on giving this to extended family that has no idea how to use it from the command prompt, and are not computer savvy
Thanks!
Assuming you're using Windows, try running the jar from a console window thats already open:
java -jar myJar.jar
This will display the output in your existing console window.
You're not seeing anything when running the jar from the explorer because it finishes too fast and closes it's window upon completion before you can properly see it.
Suppose I've created an executable jar from a code where I have used
System.out.println()
When we run the executable jar, there is no console. So, what happens to this line? How does java handle this situation?
EDIT 01:
NOTE: The situation is when I don't use a console to run the jar nor associate any console with it anyhow.
EDIT 02: Making things clearer:
I know that nothing will be printed anywhere as there is no console..! I want to know how java handle this line in this case? Is this line omitted when generating the bytecode for a executable jar? Or is this line just overlooked when there is no console? Or anything...
There's nothing special about running code in an executable jar file. If you run it from a console, e.g. with java -jar foo.jar the output will still go to the console.
If you run the code in some way that doesn't attach a console - such as javaw on Windows, which is the default program associated with executable jar files - then the output won't go anywhere. It won't cause any errors - the text will just be lost.
Note that in that case, if you use System.console() instead, that will return null. So:
System.out.printf("Foo%n"); // No problem. Goes nowhere.
System.console().printf("Foo%n"); // Would throw a NullPointerException.
The output is omitted, as long as you do not run your application from a console window (java -jar executable.jar). Furthermore, you can configure your Java installation such, that a console windows is launched as soon as you start a Java application. Output will be written to the JVM's console window then. You can find an article How do I enable and view the Java Console? on the official Java web site.
I you open it from the console (java -jar YourJar.jar) the text gets printed in your console window.
If you open it in the explorer (or similar), you won't see the text
To clarify your second Edit:
The bytecode is not omitted, because the compiler cannot know in what context the jar will be executed. The jar can always be called from console, in that case the println has to stay there.
In fact, many jar Files would be completely useless otherwise. There are plenty of Java programs that interact with the user by console in- and output.
It is not neccessary for a java-Program to have a GUI (or to run completely in the background).
So,
I have made a little game in eclipse, it generates a rondom number from 1 - 1000 , when playing out of Eclipse, it plays in the eclipse console, but what if i want to play it without eclipse?
There is no console showing up?
Anyone a idea on how to fix this?
EDIT : I would like a new GUI instead of CMD.
Grts
PS: Tell me if you need a specific part of the source code
1) You should probably export your program as a .jar file.
This isn't absolutely necessary - but it's better. And it sounds like you've already done it :)
2) Be sure to specify the main class in Eclipse before you export the .jar
3) Once you have the .jar, you can:
a) execute "java -jar myjar.jar" from a command prompt (for a console-mode program)
b) execute "javaw2 -jar myjar.jar" from a command prompt (for a Swing/GUI program)
4) You can also set a "file association" with javaw in Windows so that you can just double-click on the .jar to execute it:
Running JAR file on Windows
It seems the OP wants to reroute console output to a location of his choosing, not open an actual shell.
Here is one way to reroute stdout.
You can send it to a string, stream, file, etc. and then print it yourself into a window.
If you want a GUI, you will have to develop one yourself. There is no "Java Console GUI" that I've ever heard of.
I encountered this problem several times. If I pack a Java application in an executable jar file that takes arguments from a user, then the user have to invoke the program (jar file) from the command prompt by the following command:
java -jar "jar-file-name.jar"
But I want that whenever a user double clicks on the executable jar file (that needs arguments from the user), a window (command-prompt window) appears that would appear if we had invoked the jar file from the command-prompt.
I know one solution to this is using batch file .bat to run the jar file. Is there any other solution?
To explain why:
There are 2 java JVM exe launchers:
java.exe: console based - provides console input/output.
javaw.exe: for GUI apps - hides the console.
JAR file extensions are associated with javaw.exe by default, which is why you don't get a console when you double-click them.
The answers others have given, and adding my own:
rewrite your app so that it uses Java GUI items for input and output instead of System.in/System.out. This may be over-complex for what you require.
You mentioned creating a batch file so that the console-based Java JVM (java.exe) is run, You could also create a windows shortcut specifying the command line: java -jar jar-file-name.jar
You could change the windows file associations for .jar (but generally this is a bad idea -- new Java installs may reset this, and it will mean all java apps run from jars will have a console)
You could use a Java launcher like WinRun4J which allows you to simply drop a double-clickable EXE with an icon and a config file that specifies how your app should be run (with/without console, and with any other JVM and command line parameters )
Personally I went for the last option in my project - I made my jar file non-executable, and the user has to double-click the EXE. It also allowed me to specify a nice icon for my project, and provide multiple options on launch (debug/non-debug mode) simply by having a different exe/config file.
You would need to reassign the explorer association for ".jar" to java.exe instead of javaw.exe. This is somewhat of a questionable thing to do - it might make more sense to create your console output window. You can, of course, trivially pop up a dialog for user input of required execution parameters if they are not supplied on the command line.
There is working code for a console output JTextArea here.
This is an operating system feature.
If you want full control, then you must provide this functionality inside your program (or with a wrapper class.