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

时间:2014-12-25 11:15:55

标签: java javascript html

我正在尝试使用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>

我试图使用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);
    }
}

但是这个程序会做任何事情并抛出以下错误

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

有没有更好的方法从Java调用该html程序并在浏览器上显示该HTML? 任何人都可以帮助我

1 个答案:

答案 0 :(得分:2)

ScriptEngine::eval仅执行javascript脚本。它不会处理HTML。

您可以使用 Desktop::browse

在浏览器中打开的Html页面
Desktop.getDesktop().browse(new File("map.html").toURI());    

P.S。您得到NullPointerException,因为代码中变量enginenull
要获取非空engine,请使用,例如nashorn名称

manager.getEngineByName("nashorn");  

"html"默认情况下是::getEngineByName的无效参数(当然,您可以使用方法::registerEngineName注册自定义引擎,但您不需要它)