Make an executeble JAR run in a console when double clicked - java

I'm in the process of porting simple console apps written in C++ to Java in order to show how similar the languages are, and how code can be ported. I've exported executable JARs for the programs using Eclipse. These JARs work fine when they are run using the java -jar command. However, they do not run properly when executed from windows explorer (double clicked). A console window simply opens and closes rapidly.
The only solution I have found thus far is to use a batch file. Is there any other option?

There are some options:
Sleep a bit when the program is done, so you can read the output.
Call System.in.read() (again at the end of the program), which will wait until you type a char and hits enter.
Create your own little swing terminal, which doesn't close when the application code is done.

This seems like the jre used from the command line and from the registry are not the same (this can occur when multiple JRE's are installed)
Have you checked in your windows registry what is the jre used to run the executable jars ?
You can find more on the registry settings on sun's site

Seems to me that your main class does not even gets executed.
This would explain the quick open/close of console window.
Or is it possible that another program than java has the "jar" file type?
Happened to me after installing a Nokia software.

Using a batch file seems to be my best option. I suppose that's why Eclipse has a launcher.

Change the open with program to
c:\Program files\java\jre7\bin\javaw.exe

Related

Running a java program in VScode

I start coding java with VScode recently. I have a simple problem. The problem is that I do not want the output to be like that. In other words, I do not want the program to run in the terminal as shown below. I just want the statements to be printed alone. Here, I just want (Hello world) only to be printed with nothing else. I googled how to run a java program and I found that the output must appear in either OUTPUT or DEBUG CONSOLE.
Can anyone please help me?. NOTE: I installed java 14 as well as all needed extensions in VScode for java.
If your code needn't input data, you can add this in the launch.json:
"console": "internalConsole"
The default value is:
"console": "integratedTerminal"
I do not suggest install the 'Code-Runner' extension, because it will compile the java file under the same folder of the java file, and mix them up.
And I recommended you to get used to the outputs, the outputs can provide useful information, let you know what's exactly the vscode does. If you run into some problems, you will need this information to help you to solve the problem.
In this case, you can just press Command F5 (Mac) or other keybindings (https://code.visualstudio.com/docs/getstarted/keybindings). This runs the program in your IDE. Otherwise, you can create a launch.json file to configure your debugging.
What you want to do is follow the instruction in Run and Debug Java in VS Code to setup a launch.json file. This defines how your code is launched and where the output from that debug session goes. You can customize the Debug output that appears by adding and configuring the User Settings for the Microsoft Java Debugger
Here are some things which you can do:
If you have installed all necessary extension packs, then the editor would display an option to Run & Debug the class on the code itself. (rare cases but it appeared while working in Linux)
Else type javac <name.java> in the terminal(output will be displayed in Terminal tab)
Or install the code runner extension which is available in VS Code Marketplace (that would display results in output tab easily)

How to automatically clear the terminal after every code run in VS Code (No Code Runner, No modifying a Java file)

I'm interested in being able to automatically clear the terminal every time I run my code (Java program with a main method that may or may not have packages inside the file). Code Runner only runs single class Java files and doesn't work with Java files that have packages so I'm not interested in that.
At the momement I'm running all my Java files by clicking on the "Run and Debug" button in VS Code. I'm aware that I can manually clear the console using Command + K (Mac) but doing this for every run feels repetitive so I'm looking for a way to do this automatically. Every time I click that run and debug button I'd like for it to clear the console before running an code. Is there also anyway to show a run button that maps to VS Code's "Run and Debug Button (Similar to the way Code Runner does it by displaying a run/triangle icon (left a screenshot below). Currently I have to make two clicks in order to click "Run and Debug".
I'm only looking for a solution that doesn't involve changing any Java files so something that involves changing settings/using an extension/related on VS code. I'm not looking to modify any Java file. Code Runner has the functionality I desire (clear terminal automatically) but doesn't work with all types of Java files so it again can't be used.
You could just put this line in your java file to clear the terminal.
For Windows:
Runtime.getRuntime().exec("cls");
For Linux:
Runtime.getRuntime().exec("clear");
If you can modify your output you can print an ANSI clear screen escape sequence before printing your first line of output.
System.out.println("\033[2J\033[1;1H");
Note, this will only work with VTE based terminals like Terminal.app, iTerm, almost all linux terminal emulators, Windows Powershell etc.

Why does JavaFX work differently outside NetBeans?

I made a Java application (JDK 1.7) using Netbeans 8.1 and JavaFX.
The basic idea is a grid filled with letters, the rows and columns are sequentially highlighted to allow a person to chose a letter with a single key press (spacebar). Once the number of characters is equal or greater than 3, I implemented an autocomplete feature that fetches word starting with those letters and ranks them by lexical frequency.
It works really well when I run the project from inside the IDE but when I try to run it outside the following happens:
when I start the program, input doesn't work for a long time (10-15 seconds... although sometimes it works almost instantly and sometimes it takes even more time)... but once it starts detecting my key presses again, it works 100% of the time. It's not limited to the spacebar (also "+" and "-" used to manipulate the speed of the highlighting). Also, the program isn't frozen, I can resize stuff just fine and the row/columns are animated.
I also put a label next to the row where I put my autocomplete suggestions. Said label is supposed to be tilted -90°. Whenever I run it from outside Netbeans, it loses its rotation. Note: I don't use a CSS file, I used .SetStyle().
How can it behave differently just because I don't run the application from Netbeans?
Thank you for your answer!
Some possible reasons:
The .jar file is not up to date.
Netbeans runs from the .class files while running externally uses the .jar file.
Do a clean and rebuild and try again.
Different classpath.
Netbeans specifies the classpath to all the libraries it wants the app to use on the command line. What command line are you using? Are there any differences?
You can see the command line used in the output window when Netbeans runs your app (you may need to enable a verbose form of output - with Maven you can add --debug).
Security problems that take some time to resolve if you have reduced privileges.
Many people give Netbeans administrator rights (allows it to create jar files for example). This may not be the case on the command line.
Try running the .jar from a command prompt with admin rights.
(Inspired by the actual problem - thank you #BlackRainbow) - The Netbeans project will be bound to a specific Java version. When run from the command line using just Java ... -jar ... you will get the latest Java version which may be different.
Try running it from the command line selecting a specific version of Java. Something like "C:\Program Files\Java\jdk7\jre\bin\java" ... or similar.

Why do I see Eclipse as 'javaw.exe' in Windows task manager?

Sorry if this is too naive, but shouldn't it be something like eclipse.exe?
The links provided in the comments are quite informative, but if you still aren't able to understand why Eclipse shows as javaw.exe in the Task Manager, then refer to this answer. (Excerpt below)
javaw: (java windowed) Application executor not associated with console. So no display of output/errors. Can be used to silently push the output/errors to text files. Mostly used to launch GUI based applications.
Eclipse is a GUI based development tool and hence the process associated with Eclipse reads javaw.exe in Task Manager.
If you need it to be run as eclipse.exe, you can pass in the following parameter in your eclipse configuration file:
-vm <your java installation folder>\bin\client\jvm.dll
Eclipse is a program, which runs upon Java Virtual Machine. javaw.exe is a process, corresponding to JVM.

Opening a .jar in Ubuntu

While making a game, i moved from programming in windows to ubuntu for better usability. that aside, when in windows, using Desktop d = Desktop.getDesktop(); and using d.open(new File("file dir.jar"); and it worked in windows. but, when using ubuntu, it opens the jar in an archive manager. so my question is, how would i open the .jar to be executed? Any help would be great! thanks in advance!
Edit: it may be possible i installed java wrong... but i did it the way i found on google. if that is the case, any correction would be great :D
I see two ways:
1) add the jar you want to run to the program classpath and run it's main method.
2) using Runtime.exec() you can run anything you want, but you may lose platform independence
The archive manager is set as the default program to open .jar files. You will have to change it in Ubuntu's settings for default programs. You can right-click on the file you want to open and select to open it with your installed Java runtime environment, in the meantime.

Categories

Resources