How to know if is Applet or Application - java

I have this code inside a class that is used by an application and by an applet.
static
{
if (System.getProperty("os.name").startsWith("Windows"))
{
System.loadLibrary("extmapi");
}
}
Unfortunately, when the applet loads this code I get an error, because it can't load the "extmapi" library.
To avoid this error, I need to know if the code I'm running is an Applet or an application, so that I can do:
if (isApplet)
return;
else
//load library
How can I know if I'm running inside an Applet?

Can't you just catch the (Security?) exception?

Your top-level container will be an instance of Applet.
if (thispanel instanceof Applet)

Related

How to let run only one instance of application at a time?

I am working on a GUI application that uses JavaFX(not fxml) and exported as a JAR. For slow machine, impatient user click more than once on JAR, and multiple instances of application started.
I'm looking for a solution to let only one instance can be run at a time on a system and if the user clicks again while the application is running nothing happens. I think it's called singleton but don't know how to implement it.
You could try JUnique. It's an open source library doing exactly what you ask for. Import junique-1.0.4.jar to your project as a library. It's just 10kb file.
It's manual neatly describes how to implement it on a project. For a JavaFX application, implementation would look something like this:
Make sure to import these classes to your main
import it.sauronsoftware.junique.AlreadyLockedException;
import it.sauronsoftware.junique.JUnique;
public static void main(String[] args) {
String appId = "myapplicationid";
boolean alreadyRunning;
try {
JUnique.acquireLock(appId);
alreadyRunning = false;
} catch (AlreadyLockedException e) {
alreadyRunning = true;
}
if (!alreadyRunning) {
launch(args); // <-- This the your default JavaFX start sequence
}else{ //This else is optional. Just to free up memory if you're calling the program from a terminal.
System.exit(1);
}
}
One easy solution that I've used is, when you start the application, it creates a file (I named it .lock but you can call it whatever you want), unless the file already exists, in which case the application terminates its execution instead of creating the file.
You will need to bind your application with a resource. It can be a file, port etc.
You can change the code on startup to check if the file is locked. The below code will give you some idea
FileOutputStream foStream = new FileOutputStream("/tmp/testfile.txt");
FileChannel channel = fileOutputStream.getChannel();
FileLock lock = channel.lock();
If you'd properly package your JavaFX code as a real application instead of just throwing it into a jar, you might get that functionality for free and without all these hacks. If I package my JavaFX code on my Mac with the jpackage tool, the result will be a full featured macOS application. That means that when I double-click its icon somewhere several times, only one instance of the application will be started. This is the default behaviour on Macs and properly packaged JavaFX applications just stick to that rule too. I can't say however what the behaviour on Windows or Linux is because I currently don't have such a box running. Maybe someone who knows can add this as a comment.

Is it possible to register a custom webdriver in a Selenium grid?

I created a custom webdriver (for a custom browser) by extending RemoteWebDriver. I can easily use it as standalone, by simply instantiating the driver.
But actually I want to use it in a Selenium Grid. Is there a way to register this custom web driver on a node, so that I can use it with via RemoteWebDriver and desired capabilities? I so, what do I need to do.
Any hint is welcome. Thanks in advance.
The WebDriver (server) variants are specifically designed/created/modified continously to be able to drive the ever evolving Web Browsers.
So if you want to drive a Custom Browser through a Custom Webdriver, it seems to be the perfect approach.
At this point, it is not clear from the question if your usecase resembles as a case where you don't actually want a browser.
However, as per the configuration in Browser.java the following set of Browsers are extensively tested before any release:
package org.openqa.selenium.testing.drivers;
import java.util.logging.Logger;
public enum Browser {
chrome,
edge,
ff,
htmlunit,
ie,
none, // For those cases where you don't actually want a browser
opera,
operablink,
safari;
private static final Logger log = Logger.getLogger(Browser.class.getName());
public static Browser detect() {
String browserName = System.getProperty("selenium.browser");
if (browserName == null) {
log.info("No browser detected, returning null");
return null;
}
try {
return Browser.valueOf(browserName);
} catch (IllegalArgumentException e) {
log.severe("Cannot locate matching browser for: " + browserName);
return null;
}
}
}
Solution
To make a provision for your own Custom Webdriver and Custom Browser you may need to add the relevant entries within Browser.java and other required files and you will be good to go.
Please follow the below set of instructions to setup support for a custom browser type when running selenium tests against a Selenium Grid.
Implement the interface org.openqa.selenium.remote.server.DriverProvider (or) extend org.openqa.selenium.remote.server.DefaultDriverProvider wherein you take care of building support for your custom browser.
Create a directory named META-INF\services under your main resources folder and ensure that this directory gets bundled into the jar, when you create a jar out of your project.
Create a service loader file named org.openqa.selenium.remote.server.DriverProvider wherein you add the fully qualified class name of the new class you created in step (1) and place it in the directory created in step (2)
Bundle your project into a jar.
Now start the selenium node by adding the jar created in (4).
Now your new browser is ready to be supported by the Selenium Grid.
Please refer to this selenium-users google forums thread which also talks about the same query wherein the user confirmed that the above mentioned approach worked for them.
You still need to take care of creating a new custom capabilities object from your client side when you instantiate the RemoteWebDriver object for your custom browser.

Open a File with Desktop Application with Applet

somehow I want to open a file form a web application with the desktop application from client side.
My boss told me to use Applet. I've been through all the internet could provide me, but still can't find how to do it.
I've build a code program from java class to open the file directly but I can't make the applet running from JSP file.
Here's my code :
public static void main(String[] a) {
try {
URI uri = new URI("your/local/file/path");
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
if (desktop != null)
desktop.browse(uri);
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (URISyntaxException use) {
use.printStackTrace();
}
}
If somebody ever done it before, I'll be really thankful.
Normally, Applets do not have access to the local file system due to security issues. However, there are ways to grant file system access to applets. This article describes the procedure: https://www.developer.com/java/other/article.php/3303561/Creating-a-Trusted-Applet-with-Local-File-System-Access-Rights.htm. Altough I have not tested it myself and it is rather old, the article seems promising. I hope, this helps you.

How to implement printing in chrome pacakaged app created from GWT Application.?

I have integrated the GWT application with Chrome packaged app with help of DirectLinkerinstaller like the code below:
public class CSPCompatibleLinker extends DirectInstallLinker {
#Override
protected String getJsInstallLocation(LinkerContext context) {
return "com/google/gwt/core/ext/linker/impl/installLocationMainWindow.js";
}
}
But now I want to call print function from Chrome packaged app. When I call window.print() it allows me to print current window, but I need to open a new separate window and print that.
Could you anyone please help me in this?
I can't answer anything about GWT or DirectLinkerinstaller, but here's an answer about Chrome Apps, assuming that's what you're asking about:
You use the chrome.app.window.create API to create a window. Then, you can call the print method for that window.
In my apps, I seldom want to print what's in a window, but rather something I've generated specifically for printing. For that, I create a PDF with jsPDF (Google it), which works well. Then I display the PDF in a window, and let the user print the PDF (or save it).

Java GUI - Web Browser and Opening Link

I am creating a game and I made a Launcher. I have seen on other games made out of Java (Like MineCraft) have a webpage on the launcher. I was woundering how to put a webpage on a Java Swing GUI panel. I would also like to know how to open their browser up to a link with a button.
Thanks,
Blockquote
To open a url in the system's web browser you can use java.awt.Desktop.browse(URI). This allows you to keep your Java code platform independent, and even allows you to check to see if an operation is supported before trying to use it.
To load a web page within Java, I've had some success using the JavaFX WebView.
import java.awt.Desktop;
import java.net.URI;
class URLBrowsing
{
public static void main(String args[])
{
try
{
// Create Desktop object
Desktop d=Desktop.getDesktop();
// Browse a URL, for example www.facebook.com
d.browse(new URI("http://www.facebook.com"));
// This open facebook.com in your default browser.
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}

Categories

Resources