{"id":1000,"date":"2019-08-03T12:48:56","date_gmt":"2019-08-03T10:48:56","guid":{"rendered":"https:\/\/www.bartbarnard.nl\/blog\/?p=1000"},"modified":"2019-08-03T13:14:27","modified_gmt":"2019-08-03T11:14:27","slug":"running-program-in-intellij-with-language-level-11-and-obtaining-api-key","status":"publish","type":"post","link":"https:\/\/www.bartbarnard.nl\/blog\/running-program-in-intellij-with-language-level-11-and-obtaining-api-key\/","title":{"rendered":"Running program in IntelliJ with language level 11 (and obtaining api key)"},"content":{"rendered":"<p>This week, I started studying the course<a href=\"https:\/\/www.coursera.org\/learn\/advanced-data-structures\/\"> Advanced Data Structures in Java on Coursera<\/a>. Partly because I wanted to brush up my data structure knowledge, partly because the summertime is always a good period to do some study.<\/p>\n<p>The running application at this course is a Google Maps API, of which the start up code was provided as a downloadable zip. This zip, as the lectures, assume you are using Eclipse as an IDE, with Java language level 8. Last September, however, I switched from Eclipse to IntelliJ, using language level 11. As I did not want to reinstall either Eclipse or Java 8, I decided to spend an afternoon porting the project to IntelliJ with level 11.<\/p>\n<p><strong>pom.xml<\/strong><br \/>\nThe project uses JavaFX, which on OS-X requires a lot of dependencies. The first task I set myself to, was to create a pom.xml file so that all those dependencies would be installed by Maven. However, it turned out that not all required jars were available as maven-repo, and that JavaFX also required something called Dynamic Libraries, which are not supported by Maven (though perhaps some other dependency manager could be of help in this regard).<\/p>\n<p>Later, when the time came to run the project, another reason not to use maven to install the jars became apparent, so I ended up making a separate lib-directory with all the jars and dylibs in it. The contents of this directory is <a href=\"mandarin.nl\/presentaties\/javafx_lib.zip\">provided in this zip-file<\/a>.<\/p>\n<p><strong>Starting with IntelliJ<\/strong><br \/>\nFirst create a new plain Java project in IntelliJ and copy the lib-directory to the root of this new project. After that, copy the contents of the src-directory of the downloaded zip to the src-directory of this new project. Next to the lib-directory, copy the data-directory, which is also provided. So the directory-structure of the project looks as follows:<br \/>\n<img loading=\"lazy\" class=\"aligncenter size-medium wp-image-1001\" src=\"https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/directory_listing-300x208.png\" alt=\"\" width=\"300\" height=\"208\" srcset=\"https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/directory_listing-300x208.png 300w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/directory_listing-768x532.png 768w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/directory_listing-1024x709.png 1024w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/directory_listing-945x654.png 945w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/directory_listing-600x415.png 600w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/directory_listing.png 1416w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><br \/>\nTo make the jars at the lib-directory available to the java-classes that use them, you have to add them all to the project as library. Go to <tt>Project Settings -&gt; Libraries<\/tt>, hit the addition sign and navigate to the lib-directory with all the jars (you could of course put all actual jars at a different place and just provide the project with the correct directory, but this is my personal preference).<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-medium wp-image-1002\" src=\"https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/Libraries-300x182.png\" alt=\"\" width=\"300\" height=\"182\" srcset=\"https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/Libraries-300x182.png 300w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/Libraries-768x467.png 768w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/Libraries-1024x623.png 1024w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/Libraries-945x575.png 945w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/Libraries-600x365.png 600w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Now, when you try to build the project, sometimes IntelliJ will complain about the language level. There are actually three places where you must provide the language level: two times at <tt>Project Settings -&gt; Project<\/tt> and once at <tt>Preferences -&gt; Build, Execute, Deployment -&gt; Compiler -&gt; Java Compiler<\/tt> (this last place has &#8216;Same as language level&#8217; as a default, but in my experience it&#8217;s better to make the level explicit). Now, you should be able to build the project without any Java errors.<\/p>\n<p><strong>Running with language level 11<\/strong><br \/>\nNow if you navigate to MapApp.java and try to run the static void main, you will encounter an IllegalAccessError. This is due to the fact that starting with level 9, Java uses a stronger module system that encapsulates all the packages. As a consequence, only modules that ship with the JDK (basically java.* and javax.*) are accessible without hassle; all other packages must be explicitly named (see <a href=\"https:\/\/blog.codefx.org\/java\/java-9-migration-guide\/#Illegal-Access-To-Internal-APIs\">this blog<\/a> for a good explanaition of this situation).<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-medium wp-image-1003\" src=\"https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/error-300x71.png\" alt=\"\" width=\"300\" height=\"71\" srcset=\"https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/error-300x71.png 300w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/error-768x182.png 768w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/error-1024x243.png 1024w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/error-945x224.png 945w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/error-600x142.png 600w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>To do so, we must change the run configuration<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-medium wp-image-1004\" src=\"https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/edit_configurations-300x147.png\" alt=\"\" width=\"300\" height=\"147\" srcset=\"https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/edit_configurations-300x147.png 300w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/edit_configurations-768x378.png 768w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/edit_configurations-1024x503.png 1024w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/edit_configurations-945x465.png 945w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/edit_configurations-600x295.png 600w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/edit_configurations.png 1074w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>and must add the following VM options:<br \/>\n<tt><br \/>\n--module-path .\/lib<br \/>\n--add-modules javafx.controls,javafx.fxml,javafx.web<br \/>\n<\/tt><br \/>\nThis tells the VM that the modules are located in the .\/lib-directory and which modules in that directory it must access (there is some kind of recursive system that prevents us from having to name all the jars explicit). Now, if we were to use maven after all, instead of <tt>.\/lib<\/tt> we could point the VM to the directory where maven puts its downloaded jars (on my system, that would be <tt>~\/.m2\/repository\/<\/tt>), but since we need to manually include the dylibs anyway, I didn&#8217;t bother to change this (even though in fact it would be the better architecture \u2013 for those interested <a href=\"http:\/\/mandarin.nl\/presentaties\/pom.xml\">I provide the pom.xml<\/a>).<\/p>\n<p>After these changes, you should be able to start the application.<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-medium wp-image-1005\" src=\"https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/application_without_key-300x177.png\" alt=\"\" width=\"300\" height=\"177\" srcset=\"https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/application_without_key-300x177.png 300w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/application_without_key-768x454.png 768w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/application_without_key-1024x605.png 1024w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/application_without_key-945x559.png 945w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/application_without_key-600x355.png 600w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><strong>Obtaining the Google API key.<\/strong><br \/>\nThe last challenge before setting off to the actual assignments is to have the google maps api work in this web browser. Head over <a href=\"https:\/\/developers.google.com\/maps\/documentation\/javascript\/get-api-key\">to google developers<\/a> and read the documentation.<\/p>\n<p>You need to have a Google account in order to be able to obtain an API key. Head over <a href=\"https:\/\/cloud.google.com\/console\/google\/maps-apis\/overview\">to the console view<\/a> and click on the hamburger menu on the top left. Click on Projects and Create New Project. Next, select the project and click on &#8216;APIs and Services&#8217; and select &#8216;Credentials&#8217;. On the screen that appears, click &#8216;Create Credentials&#8217; and select &#8216;Api Key&#8217;. This will create a key that you can use in the application. Remember to restrict the use of the key (for this course, restricting on IP address seems the most logical option).<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-medium wp-image-1007\" src=\"https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/new_project_google-300x197.png\" alt=\"\" width=\"300\" height=\"197\" srcset=\"https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/new_project_google-300x197.png 300w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/new_project_google-768x503.png 768w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/new_project_google-1024x671.png 1024w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/new_project_google-945x619.png 945w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/new_project_google-600x393.png 600w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-medium wp-image-1008\" src=\"https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/google_api-300x134.png\" alt=\"\" width=\"300\" height=\"134\" srcset=\"https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/google_api-300x134.png 300w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/google_api-768x343.png 768w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/google_api-1024x458.png 1024w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/google_api-945x422.png 945w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/google_api-600x268.png 600w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/google_api.png 1318w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>It seems that the key is restricted to one call per day, which is a bit too harsh for development purposes. However, it is possible to get $300 worth of data storage and traffic. For this, you need to supply Google with billing information and a credit card number. According to the docs, this step is only needed to verify that you are not a robot; charging will only take place after a manually selected update of the free account.<\/p>\n<p>Once you have the API key, copy this in the file index.html, in the directory html. Once you have done this, everything should run smooth and you can at long last start with the first assignment.<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-medium wp-image-1006\" src=\"https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/application_with_key-300x186.png\" alt=\"\" width=\"300\" height=\"186\" srcset=\"https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/application_with_key-300x186.png 300w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/application_with_key-768x475.png 768w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/application_with_key-1024x633.png 1024w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/application_with_key-945x585.png 945w, https:\/\/www.bartbarnard.nl\/blog\/wp-content\/uploads\/2019\/08\/application_with_key-600x371.png 600w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>A better solution to this problem would be to use an open source variant, such as <a href=\"https:\/\/www.openstreetmap.org\/\">openstreetmap<\/a> or <a href=\"https:\/\/leafletjs.com\/examples\/quick-start\/\">leafletjs<\/a>, but this would require a major refactoring of the code, so that is not really an option.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This week, I started studying the course Advanced Data Structures in Java on Coursera. Partly because I wanted to brush up my data structure knowledge, partly because the summertime is always a good period to do some study. The running application at this course is a Google Maps API, of which the start up code<\/p>\n<p class=\"more-link\"><a href=\"https:\/\/www.bartbarnard.nl\/blog\/running-program-in-intellij-with-language-level-11-and-obtaining-api-key\/\" class=\"themebutton2\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":1003,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/www.bartbarnard.nl\/blog\/wp-json\/wp\/v2\/posts\/1000"}],"collection":[{"href":"https:\/\/www.bartbarnard.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bartbarnard.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bartbarnard.nl\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bartbarnard.nl\/blog\/wp-json\/wp\/v2\/comments?post=1000"}],"version-history":[{"count":2,"href":"https:\/\/www.bartbarnard.nl\/blog\/wp-json\/wp\/v2\/posts\/1000\/revisions"}],"predecessor-version":[{"id":1011,"href":"https:\/\/www.bartbarnard.nl\/blog\/wp-json\/wp\/v2\/posts\/1000\/revisions\/1011"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bartbarnard.nl\/blog\/wp-json\/wp\/v2\/media\/1003"}],"wp:attachment":[{"href":"https:\/\/www.bartbarnard.nl\/blog\/wp-json\/wp\/v2\/media?parent=1000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bartbarnard.nl\/blog\/wp-json\/wp\/v2\/categories?post=1000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bartbarnard.nl\/blog\/wp-json\/wp\/v2\/tags?post=1000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}