简体   繁体   English

在Java程序中使用javascript调用HTML程序

[英]Call a HTML program with javascript in a Java program

I am trying to run a HTML file with javascript from a java program . 我正在尝试从Java程序使用javascript运行HTML文件。

Map.html
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Circles</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>

   <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script>
// This example creates circles on the map, representing
// populations in North America.

// First, create an object containing LatLng and population for each city.
var citymap = [
      ['Chicago', 41.878113, -87.629798, 4],
      ['New York', 40.714352, -74.005973, 5],
      ['Los Angeles', 34.052234, -118.243684, 3],
      ['Vancouver',49.25, -123.1, 2],
      ['Oregon', 45, -120, 1]
    ];
var cityCircle;

function initialize() {
  // Create the map.
  var mapOptions = {
    zoom: 4,
    center: new google.maps.LatLng(37.09024, -95.712891),
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);

  // Construct the circle for each value in citymap.
  // Note: We scale the area of the circle based on the population.
      var fillcolor=[];
      fillcolor[0]='#FF0000';fillcolor[1]='#FFFF00'; fillcolor[2]='#FF00FF';    fillcolor[3]='#00FF00';
      var loop=0;
     for (i = 0; i < citymap.length; i++) {  
    var populationOptions = {
      strokeColor: '#000000',
      strokeOpacity: 0.8,
      strokeWeight: 2,

      fillColor: fillcolor[loop],
      fillOpacity: 0.35,
      map: map,
      center: new google.maps.LatLng(citymap[i][1], citymap[i][2]),
      radius: Math.sqrt(citymap[i][3]) * 100000
    };

    // Add the circle for this city to the map.
    cityCircle = new google.maps.Circle(populationOptions);
   // cityCircle = new google.maps.Circle(populationOptions1);
    loop=loop+1;


  }
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

This program I am trying to call in the following program using ScriptEngine:- 我正在尝试使用ScriptEngine在以下程序中调用此程序:

public class RunJs {
public static void main(String[] args) throws IOException, JSONException, ScriptException {
        // TODO Auto-generated method stub
          ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("html");
           FileReader file = new FileReader("map.html");
            engine.eval(file);
    }
}

But this program doenst do anything and throws the below error 但是此程序确实做任何事情并抛出以下错误

Exception in thread "main" java.lang.NullPointerException
    at google.api.search.RunJs.main(RunJs.java:28)

Is there any better way to call that html program from Java and display that html on a browser ? 有没有更好的方法从Java调用该html程序并在浏览器中显示该html? Can anyone please help me 谁能帮帮我吗

ScriptEngine::eval executes only javascript script. ScriptEngine :: eval仅执行javascript脚本。 It doesn't process HTML. 它不处理HTML。

Html page your can open in browser with Desktop::browse : 您可以使用Desktop :: browse在浏览器中打开的HTML页面:

Desktop.getDesktop().browse(new File("map.html").toURI());    

PS You get NullPointerException , because variable engine is null in your code. PS您会收到NullPointerException ,因为变量engine在您的代码中为null
To get not null engine , use, for example nashorn name 要获取非null engine ,请使用例如nashorn名称

manager.getEngineByName("nashorn");  

"html" is invalid param for ::getEngineByName by default (of course you can register your custom engine with method ::registerEngineName but you don't need it) 默认情况下, "html":: getEngineByName的无效参数(当然,您可以使用:: registerEngineName方法注册自定义引擎,但不需要它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM