Java console and GUI mode of application? - java

What is the best way to create application that can be used both as GUI app and console tool?
Here is relative info: Can one executable be both a console and GUI application?
My question is more about Java, Maven, JavaFX - my app currently runs as JavaFX GUI application.
P.S. Any relative help, very appreciated. I am totally confused.

Probably do this:
Add an option like java -jar MyApp.jar -gui
Fall back to console if no GUI is available, e.g. Java runs in headless mode
Use the Console class for the console ui.

Related

Is there a way to override system name for Java app?

There is a Java Web Start application that I'm trying to run under Java on Linux. I'm using icedtea-netx on Ubuntu Linux. When I try to run the application, it starts up, shows the splash screen, then produces an error saying:
"Currently the application only supports the following system: Windows".
So I'm wondering, is there a way to set your system information in Java to pretend that it’s Windows? I understand that this program might really require some aspect of Windows, but I'd like to try anyways. Thanks.

Use java tracing facilities with a desktop application

Is there a way to turn tracing and logging on for a java application which is neither a Java Web Start nor applet type of java application? I'm talking about an application that would be executed by either double clicking on an executable jar file or launched from the command line by typing java -jar nameofjarfile.jar. I have enabled logging and tracing in the Java Control Panel but this seems to have no effect. The only trace logs that I see are trace logs generated for execution of the java control panel. As far as I can discern from the documentation the options in the java control panel to enable logging and tracing are specific to Web Start and applet style applications. When I launch my desktop java application no .trace file is generated.
Thanks in advance.
It appears there is no equivalent to the trace option offered in the Java Control Panel for apps that are launched using the regular virtual machine. The options in the Java Control Panel are specifically for Java Web Start Apps and Java Applets. It has no effect on the Java Desktop Applications launched by double clicking an executable jar file or by typing java -jar javaapp.jar at the command line. While the documentation states that tracing is output from the java console to a .trace file the console to which they speak is the Java Console that is only available for Web Start and Applets. They are not speaking of just standard out and standard error. While both standard out and standard error does get output to the Java Console the Java Console also includes boot strap information of the JVM itself such as the java version, the exact path of the java executable file, proxy information and much more. I'm sure there may be a way to generate equivalent data it can not be done through the Java Control Panel's trace and log options or with Deployment Property options such as -DDeployment.trace = true. You can see the information I'm speaking of by going to the java tutorials and launching one of the many web start apps they link to in their tutorials. Make sure to go to the Java Control Panel and tick the Show Console option under the advanced tab. When you launch a Java Web Start App with this option selected the Java Console will open. The output to that console is what is dumped to the .trace file when Enable Tracing is selected in the same Advanced tab of the Java Control Panel. If you also enable logging it appears that console output is output to a .log file but in an XML format.

Running Netbeans platform application without a GUI

My Java application built using Netbeans platform application. I need to run my application without a GUI.
Anyone knows a way to run Netbeans platform application without a GUI ?
Information gathered largely from How Can I Make My NetBeans Platform Run in GUI or Command-Line Mode? :
you will typically need to add a module to interpret some custom command-line arguments using the Command Line Processing API
Remember that you should not use System.out, System.err and System.in for the output, error and input streams in the options processor but instead get them from the Env object passed as a parameter to the process method.
When running a platform application which contains the Window System and other GUI modules, you will also need to specify --nosplash --nogui on the command line at startup to prevent the splash screen and window system from being displayed.

Testing java web start application using jemmy

I need to create some gui tests using Jemmy but I have no idea how to launch it with javaws application.
In tutorials/examples/etc is something like that:
new ClassReference("org.netbeans.jemmy.explorer.GUIBrowser")
.startApplication();
This code opens an example window, but how can I, using ClassReference object open *.jnlp file? Or is it another way to "connect" jemmy with java web start application?
Thanks for advance.
You can achieve this by preparing special build with Jemmy included and call Jemmy from the app run in jnlp mode itself.
The solution is:
Place build files into any folder on your PC.
Add .jar files from this build to your testing project.
Open .jnlp file in text editor and search for main application class
application-desc main-class="[Main application class]"/>
Call it from you test to launch app
new ClassReference("[Main application class]").startApplication();
Now you can access elements of this Java app from test environment
You can use Jemmy with JUnit in NetBeans IDE.
For the GUI testing we are using Jemmy, a library that comes with the NetBeans IDE and is very useful for testing Swing applications.
JUnit tests that utilize Jemmy so for example:
#Test
public void JunitTest() {
JFrameOperator mainFrame = new JFrameOperator();
JTextFieldOperator textField = new JTextFieldOperator(mainFrame, "textIn");
int x = 10;
assertEquals(x, textField.getLocationOnScreen().x);
}
Adding the #Test annotation and making use of JUnit's assertEquals() and fail() if needed.
Another Approach:
You can also do it using jnlp. Already suggested #Sergey Grinev
A good example and running code is given in this link:
Snapshot:
Click the jnlp file link in webdriver, save jnlp file to disk;
Run the webstart app from jnlp;
Capture opened app and use it for test.
This process can be done by using following libraries:
netx - for running webstart application from jnlp.
uispec4j - for intercepting created webstart window and
manipulating window elements.
You can probably do the same trick with other AWT/Swing testing tool, but uispec4j allows to intercept webstart app executed from jnlp, you don't need to run the app by calling main() and you don't need to have your webstart app source code in your testing code repo.
Credit goes to tporeba
For learning more about Jemmy, you can go through this link
Jemmy Tutorial
Jemmy Samples
NetBeans Platform Test Infrastructure Tutorial

Can I connect my Java console application with GUI application?

I have Java console application but I need to create a user interface for the user interaction.
Is it possible to connect the GUI with Java console?
Not sure what the question is however,
You can use the console if you start with java.exe whether you use a GUI or not. If you use a GUI it is common practice to use javaw.exe which doesn't have a console, but you don't have to start your application that way.
On Unix there is only java ;)
You can connect jconsole ?Java Console? to either of these.
I have Java console application but I need to create a user interface for the user interaction.
If I understand you correctly, you have an application that takes input from the command line and you now need a GUI on top of it?
I'd say the easiest way to do this should be to create a new GUI app and use the exisiting application as a library. You might be able to call the console application's main method from the UI app and replace the standard input and output streams with your own implementations that connect to the GUI.

Categories