B4J Question Run a JavaScript Silently in a non-ui Program


Well-Known Member
Licensed User
Longtime User
Good Day,

I am very new at this type of methodology and before I pursue something that is actually not possible, I'd like to ask if one could run the following code in a non-ui B4J program to retrieve the co-ordinates of the current location of the laptop that it is running on and save them to a database (The database part is no problem).

Code is from w3schools.com (Link)

<!DOCTYPE html>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

var x = document.getElementById("demo");

function getLocation() {
  if (navigator.geolocation) {
  } else {
    x.innerHTML = "Geolocation is not supported by this browser.";

function showPosition(position) {
  x.innerHTML = "Latitude: " + position.coords.latitude +
  "<br>Longitude: " + position.coords.longitude;


Actually all I need is to run the <script> </script> section and pass that back to 2x variables so that I can save it in a database.

If this can be done Then
Please Help
Back to the drawing board
Pass Ideas please

Have a Lekka Day !!


Well-Known Member
Licensed User
Longtime User
Thank you guys - I appreciate and I will try !!.

Upvote 0


Well-Known Member
Licensed User
Longtime User
I have managed to run your javascript in chrome and get the values back into a non-ui b4j program.

Unfortunately, the chrome window pops up for about a second whilst it runs the javascript, so not silent.

The code I used ( needs Selenium jars )
'Non-UI application (console / server application)
#Region Project Attributes 
 #MergeLibraries: True 
 #AdditionalJar: d:/selenium/client-combined-3.141.59.jar
 #AdditionalJar: d:/selenium/libs/guava-25.0-jre.jar
 #AdditionalJar: d:/selenium/libs/okio-1.14.0.jar
 #AdditionalJar: d:/selenium/libs/okhttp-3.11.0.jar
 #AdditionalJar: d:/selenium/libs/commons-exec-1.3.jar
 #AdditionalJar: d:/selenium/libs/byte-buddy-1.8.15.jar
#End Region
Sub Process_Globals
End Sub
Sub AppStart (Args() As String)
 Log("Location is : " & asJO(Me).RunMethod("doit",Null))
End Sub
Sub asJO(o As JavaObject) As JavaObject
 Return o
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
 Return True
End Sub
#if java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.lang.Thread;
public static String doit() {
  System.setProperty("webdriver.chrome.driver", "d:/selenium/libs/chromedriver.exe");
  WebDriver driver = new ChromeDriver();
  WebElement ele = driver.findElement(By.name("b1"));
  }catch (InterruptedException e){
  Object loc = driver.findElement(By.id("demo")).getText();
  //Close the browser
  return (String)loc;
#End If
Upvote 0