{"id":9794,"date":"2012-08-25T10:49:00","date_gmt":"2012-08-25T09:49:00","guid":{"rendered":"http:\/\/touk.pl\/blog\/?guid=0c8ec7607391bcd75bbc0110dbc03054"},"modified":"2023-03-22T15:58:41","modified_gmt":"2023-03-22T14:58:41","slug":"how-to-create-a-native-java-app","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2012\/08\/25\/how-to-create-a-native-java-app\/","title":{"rendered":"How to create a native Java App"},"content":{"rendered":"<p>Recently, I stumbled upon <a href=\"https:\/\/github.com\/ivmai\/JCGO\">JCGO<\/a>, an interesting project, which translates Java 1.4 code into C.<br \/>\nIn this article, I\u00a0show how to create a native Windows app out of a small Java app.<\/p>\n<p>The Java app I will use is NetCat (<a href=\"https:\/\/github.com\/rafalrusin\/netcat\">https:\/\/github.com\/rafalrusin\/netcat<\/a>). You can download precompiled executable, netcat.exe, from\u00a0<a href=\"https:\/\/github.com\/rafalrusin\/netcat\/downloads\">https:\/\/github.com\/rafalrusin\/netcat\/downloads<\/a>.<\/p>\n<p>So the first step is to download all dependencies. I will use MinGW, MinGW GCC,\u00a0jcgo-lib-1_14.tar.gz,\u00a0jcgo-src-1_14.tar.bz2, classpath-0.93 (http:\/\/ftp.gnu.org\/gnu\/classpath\/classpath-0.93.tar.gz) and Java sources for the app with dependent libraries:\u00a0<a href=\"https:\/\/github.com\/rafalrusin\/netcat\">https:\/\/github.com\/rafalrusin\/netcat<\/a>, commons cli 1.2 (<a href=\"http:\/\/commons.apache.org\/cli\/download_cli.cgi\">http:\/\/commons.apache.org\/cli\/download_cli.cgi<\/a>). You need to put all this in the same directory, so it&#8217;ll have structure like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">auxbin\r\nclasspath-0.93\r\ncommons-cli-1.2-src\r\ndlls\r\ngoclsp\r\nsrc\r\njcgo\r\njcgo.exe\r\njcgo.jar\r\nlibs\r\nmiscsrc\r\nnetcat\r\nout\r\nrflg_out\r\nstdpaths.in<\/pre>\n<p>Then, you need to run Java to C translator by using command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">jcgo.exe -sourcepath netcat\/src -sourcepath commons-cli-1.2-src\/src\/java netcat.NetCat @stdpaths.in -d out\r\n\r\nInitializing...\r\n\r\nAnalysis pass...\r\n\r\nOutput pass...\r\n\r\nWriting class tables...\r\nCreating main file...\r\nParsed: 293 java files (2699 KiB). Analyzed: 3067 methods.\r\nProduced: 640 c\/h files (3769 KiB).\r\nContains: 1490 java methods, 4119 normal and 288 indirect calls.\r\nDone conversion in 1 seconds. Total heap size: 36572 KiB.<\/pre>\n<div><span style=\"font-family: Courier New, Courier, monospace\">\u00a0<\/span><\/div>\n<div><span style=\"font-family: inherit\">Next step is to compile it into final executable. Following command does this:<\/span><\/div>\n<div><span style=\"font-family: inherit\">\u00a0<\/span><\/div>\n<div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">gcc -DJCGO_INET -DJCGO_NOFP -DJCGO_WIN32 -DJCGO_THREADS -I src\/include\/ -I src\/include\/boehmgc\/ -I src\/native\/ out\/Main.c -o netcat.exe libs\/x86\/mingw\/libgcmt.a -lws2_32<\/pre>\n<\/div>\n<div><span style=\"font-family: Courier New, Courier, monospace\">\u00a0<\/span><\/div>\n<div><span style=\"font-family: inherit\">I used some switches, which are suitable for this particular app. For example, by default JCGO doesn&#8217;t use multithreading or networking. This has to be enabled explicitly.\u00a0<\/span><\/div>\n<div><span style=\"font-family: inherit\">\u00a0<\/span><\/div>\n<div><span style=\"font-family: inherit\">And that&#8217;s it. Now you can try out the app by calling google.com, like this:<\/span><\/div>\n<div><span style=\"font-family: inherit\">\u00a0<\/span><\/div>\n<div>\n<div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$ netcat.exe google.com -p 80\r\nConnecting to google.com port 80\r\nGET\r\nHTTP\/1.0 302 Found\r\nLocation: http:\/\/www.google.pl\/\r\nCache-Control: private\r\nContent-Type: text\/html; charset=UTF-8\r\nSet-Cookie: PREF=ID=2f3085ac38771e98:FF=0:TM=1345885031:LM=1345885031:S=8A-IkreMgCogMsey; expires=Mon, 25-Aug-2014 08:57:11 GMT; path=\/; domain=.google.com\r\nSet-Cookie: NID=63=O_QZ4bDrzYNiiE0DY8RT-34c_pGt_OZagP3gzrzqCAx_Xo2kO7s9zVrUOx7FVz4TyAEY7Wx9UhglYZSX9UHSdzT7c9mUKzfkJFp5lk5FyfiMIcKITLhgSX4__3QwEYBS; expires=Sun\r\n, 24-Feb-2013 08:57:11 GMT; path=\/; domain=.google.com; HttpOnly\r\nP3P: CP=\"This is not a P3P policy! See http:\/\/www.google.com\/support\/accounts\/bin\/answer.py?hl=en&amp;answer=151657 for more info.\"\r\nDate: Sat, 25 Aug 2012 08:57:11 GMT\r\nServer: gws\r\nContent-Length: 218\r\nX-XSS-Protection: 1; mode=block\r\nX-Frame-Options: SAMEORIGIN\r\n\r\n\r\n&lt;HTML&gt;&lt;HEAD&gt;&lt;meta http-equiv=\"content-type\" content=\"text\/html;charset=utf-8\"&gt;\r\n&lt;TITLE&gt;302 Moved&lt;\/TITLE&gt;&lt;\/HEAD&gt;&lt;BODY&gt;\r\n&lt;H1&gt;302 Moved&lt;\/H1&gt;\r\nThe document has moved\r\n&lt;A HREF=\"http:\/\/www.google.pl\/\"&gt;here&lt;\/A&gt;.\r\n&lt;\/BODY&gt;&lt;\/HTML&gt;\r\n<\/pre>\n<\/div>\n<\/div>\n<div>\n<p>I like the approach of translating Java code into C, because compared to other tools, which generate C++ code, this\u00a0is more suitable for embedded devices.\u00a0For example it is possible to generate code for iOS, because Objective C is a superset of C.<\/p>\n<p>One feature I would like to see though is to be able to use reference counting instead of full gc. This is because one of the advantages of C over Java is that it doesn&#8217;t have GC hangs. So then the programmer would have to make sure there&#8217;s no cycles in orphaned object structure.<\/p>\n<p>Update:\u00a0Ivan Maidansky, an author of JCGO, has put some interesting comments regarding this article. In particular, he is aware of some apps in Apple Store, which do this kind of translation. Also, reference counting is discouraged due to multithreading issues. These comments can be found here:\u00a0<a href=\"https:\/\/github.com\/ivmai\/JCGO\/issues\/2\">https:\/\/github.com\/ivmai\/JCGO\/issues\/2<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"Recently, I stumbled upon JCGO, an interesting project, which translates Java 1.4 code into C. In this article,&hellip;\n","protected":false},"author":35,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/9794"}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=9794"}],"version-history":[{"count":6,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/9794\/revisions"}],"predecessor-version":[{"id":15521,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/9794\/revisions\/15521"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=9794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=9794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=9794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}