ebook img

Java Everywhere: Write Once run Anywhere with DukeScript PDF

273 Pages·2015·7.69 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Java Everywhere: Write Once run Anywhere with DukeScript

Java everywhere Write Once run Anywhere with DukeScript Anton Epple This book is for sale at http://leanpub.com/dukescript This version was published on 2015-12-28 * This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. * © 2015 Anton Epple Table of Contents Preface About this book History How it works Getting started! Prerequisites Developing DukeScript apps with the NetBeans Plugin Creating DukeScript apps with Eclipse Using Maven to create a DukeScript project The Generated Projects Understanding the sample application Running the application The View Binding the View to the Model The Model Putting it all together Debugging a DukeScript application Debugging Java Code Visual Debugging Debugging on Android Devices Debugging on iOS Devices and Emulators Debugging JavaScript Code Debugging JavaScript with firebug-lite Summary Using the Knockout API Model, View, ViewModel (MVVM) The Model The ViewModel The View Defining the ViewModel Summary DukeScript, JSON and the REST Creating the project Running the application The client application Code sharing Parsing JSON Summary Workshop: DukeScript Twitter Client Prerequisites Set up the project DukeScript, JSON and the REST Creating the ViewModel A model for parsing JSON results Create a JSON Endpoint Adding Functions for the UI Observing Property changes Initializing the model The View Separating View and ViewModel in practice The state of UI Development The Setup Finding the right Designer Working with the design The result Responsive Design with CSS New CSS3 units vh and vw Testing Unit Tests Test Driven Development means “Test first” Is your UI Toolkit Test friendly? DukeScript to the rescue A little example - Unit Testing a viewmodel Unit testing server communication @OnReceive with Plain Text Type-safe Access to Received JSON Data Dealing with JSON Arrays Summary Deploying to the App Stores Deploying to the Google App store Deploying to the Apple Store Summary Creating a new API Some examples HTML5 Canvas The @JavaScriptBody Annotation The @JavaScriptResource Annotation Summary Dear Reader, Preface For many years, Java Swing enabled developers to write applications that could run on any operating system. That all came to an end with the arrival of smart phones, tablets, and embedded computers. In the enterprise, the desktop dominated for many years. In the meantime, however, almost every IT project includes plans for a future where the application will need to be ported to mobile platforms. Building native applications for all platforms requires special skills and is expensive in terms of both maintenance and development. We wanted to simplify this problem and make cross-platform development available for everyone again. That’s why we developed DukeScript. In this book you will learn how to use DukeScript and develop Java applications that run on a lot of different devices. About this book This is a book to help you with your project. If you’re an absolute DukeScript newbie, that’s perfect. The book starts by explaining the core concepts and then immediately jumps to practical exercises. We’ll go through our sample applications and inspect how they work. Along the way I’ll explain the basic workings and ideas behind the APIs. There are also some more chapters explaining just the concepts themselves especially for our most important APIs, but most of the time they are accompanied by lots of workshops and code examples. Thereby you’ll get a bit of repetition which is said to be beneficial for learning and you can put your newly acquired knowledge to a practical test. That’s the basic idea behind this book. I hope you’ll find it useful. What this book is not This book is not a reference book. You will find some nice structured information on the Knockout API, but you will not find all the APIs documented in great detail. In this book I try to explain how things work together and give practical advice on how to use it, for a detailed reference you can use the excellent API documentation. We’re really trying to make API Docs as helpful as possible. Improving this book This is a living book. That means that new chapters will be added and content will be updated. Once you’ve bought the book you’ll get these updates for free as long as the book exists. If I find any errors I’ll try to fix them in a timely manner, and if you spot any errors, let me know! It also means that if you want more detail on something, or if you miss something, you can tell me, and I can try to include it, so together we can make our book even better. History For many years Jaroslav Tulach, the original architect and founder of NetBeans, and I gave a short lecture on API design together with Thomas Wuerthinger at the Johannes-Keppler University of Linz. We always brought some of our pet projects, and in the evenings we typically ended up in some pub drinking beer and hacking until we were thrown out. In 2012 Jaroslav was really frustrated by Javas stagnation on the client. Oracle declared that Swing was no longer developed, Applets were dead, and JavaFX a half-hearted approach to create a successor. While it was obvious that Java was the better language for building tooling and large scale applications, JavaScript and web technologies were basically the only way to do cross platform development. Jaroslav was looking for a way to keep Java relevant in the browser. His solution was bck2brwsr, a Java Virtual Machine running in the browser without a Plugin. The idea was to support a small part of Javas APIs to make it a lean JVM and use HTML as the view technology. I brought a little game engine that year using JavaFX Canvas as rendering technology. And since HTML5 Canvas has a very similar API, I decided to create a Canvas API for bck2brwsr that could render either to a HTML5 Canvas or to a JavaFX Canvas. That way all my games would run in the browser. A little later bck2brwsr and the Canvas API were even used by a group in Oracle to create a proptotype of JavaFX in the browser. Unfortunately all the efforts to port JavaFX to mobile platforms and the Browser inside Oracle were canceled so this work never got released, but we were really encouraged by the performance of our work. Over time the focus changed from the browser JVM to the idea of having Over time the focus changed from the browser JVM to the idea of having HTML5 as a view technology instead of Swing or JavaFX. KnockoutJS, one of the most popular JavaScript libraries turned out to be really great for separating the View from the rest of the application. And it soon became clear that this was really useful also in Swing or JavaFX applications, because it allowed for a very clean and testable architecture. So Jaroslav adapted the bck2brwsr APIs for his work on NetBeans and created the HTML/Java APIs, which are now at the core of DukeScript. For me it was the first time I came into contact with the Model View ViewModel pattern and I was immediately hooked. The way Jaroslav had designed the HTML/Java APIs they could also be used outside of NetBeans. And he had created Service Provider Interfaces that made it relatively easy to port the technology to new platforms. All you need is a very basic JVM and a modern HTML5-renderer component. This was the start of DukeScript. I had coined this term because the inventors JavaScript had abused the term “Java” to benefit from Javas popularity in the early years. So I waneted to point out that we had put real Java represented by “Duke” the Java mascot into JavaScript. I started using this term for the whole ecosystem we created to build a community, and port DukeScript to other platforms, build tools and Maven archetypes and develop additional APIs. So it’s entirely my fault that a lot of people think that this is just another Scripting language when they first get into contact, and I hereby appologise. How it works I already mentioned that the basic requirement is a JVM and a HTML5 renderer component. Install DukeScript Wizard On the Desktop we use Oracles HotSpot Virtual Machine and as a HTML- Renderer we use the JavaFX WebView. In the Browser we use the bck2brwsr VM or TeaVM, and the Browser itself is the HTML-Renderer. Here’s a little table to show what we use on supported platforms: OS Java VM HTML5 Renderer Platform OS X, Windows, Oracle Hotspot JavaFX WebView Linux iOS RoboVM NSObject.UIResponder.UiView.UIWebView Android Dalvik/ART android.webkit.WebView Browser bck2brwsr/TeaVM Chrome, Firefox, IE… OpenJDK/JamVM/Java Embedded Chromium SE Embedded This architecture made it really easy to port to different platforms, and we can relatively easy adapt to new platforms if there is demand. But now without further ado, let’s start coding DukeScript! Getting started! It’s time to get your hands dirty. In this chapter you’ll learn everything you need to create your first DukeScript application. First I’ll show you how to install and use the NetBeans Plugin. And you’ll also learn how to use the Maven Archetypes directly. This is important if you’re required to use an IDE other than NetBeans. I’ll also show you some tricks how to best integrate DukeScript Development with Eclipse. After this chapter you’ll know anything you need to create a new DukeScript application. Prerequisites For developing DukeScript applications you need at least JDK 7 and Maven 3.2.5. The best IDE for developing DukeScipt is NetBeans which provides some extra features like hot swapping of code and visual DOM Inspection, but you can also use Eclipse. For developing Android applications you need to install the Android SDK. Initially the SDK doesn’t include all the required parts. You also need to add some SDK packages. You should at least download the latest tools and Android platform 19. To do so, launch the SDK Manager. Now open the Tools directory and select “Android SDK Tools”, “Android SDK Platform-tools”, and “Android SDK Build-tools (highest version)”. Now Open the first Android 19.0 folder and select “SDK Platform”. If you have no real Android device, you should also download a system image for the emulator, such as ARM EABI v7a System Image. For building for iOS you need XCode 6 and a Mac running OS X 10.9 or later. Developing DukeScript apps with the NetBeans Plugin The easiest way to develop, debug and deploy DukeScript applications is to use NetBeans and it’s DukeScript Plugin. There are a lot of extra features we’ve put into that, so we strongly suggest to use it. Throughout the book we’ll be using NetBeans for most tasks. Still, if you don’t want, or aren’t allowed to use it, you can still use our Maven Archetypes. In that case you can skip this section.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.