simpleapplication_from_the_commandline.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]--><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="Asciidoctor 1.5.4"><meta name="keywords" content="documentation, install"><title>Starting a JME3 application from the command-line</title><link rel="stylesheet" href="./asciidoctor.css">
  2. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  3. <link rel="stylesheet" href="./coderay-asciidoctor.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css"><link rel="stylesheet" href="/home/travis/build/jMonkeyEngine/wiki/build/asciidoc/html5/jme3/twemoji-awesome.css"></head><body class="article toc2 toc-left"><div id="header"><div id="toolbar"><a href="https://github.com/jMonkeyEngine/wiki/edit/master/src/docs/asciidoc/jme3/simpleapplication_from_the_commandline.adoc"><i class="fa fa-pencil-square" aria-hidden="true"></i></a><a href="https://github.com/jMonkeyEngine/wiki/new/master/src/docs/asciidoc/jme3/"><i class="fa fa-plus-square" aria-hidden="true"></i></a><input dir="auto" style="position: relative; vertical-align: top;" spellcheck="false" autocomplete="off" class="searchbox__input aa-input" id="doc-search" name="search" placeholder="Search in the doc" required="required" type="search"></div><h1>Starting a JME3 application from the command-line</h1><div class="details"><span class="author" id="author"></span><br><span id="revnumber">version ,</span> <span id="revdate">2016/03/17 20:48</span></div><div id="toc" class="toc2"><div id="toctitle">Table of Contents</div><ul class="sectlevel1"><li><a href="#installing-the-jme3-framework">Installing the JME3 Framework</a></li><li><a href="#installing-with-git-easy-way">Installing with Git (easy way)</a></li><li><a href="#manual-download-hard-way">Manual Download (hard way)</a></li><li><a href="#sample-project-directory-structure">Sample Project Directory Structure</a></li><li><a href="#libraries">Libraries</a><ul class="sectlevel2"><li><a href="#sample-code">Sample Code</a></li></ul></li><li><a href="#build-and-run">Build and Run</a></li><li><a href="#recommended-asset-directory-structure">Recommended Asset Directory Structure</a></li><li><a href="#next-steps">Next Steps</a></li></ul></div></div><div id="content"><div id="preamble"><div class="sectionbody"><div class="paragraph"><p>Although we recommend the jMonkeyEngine <a href="../sdk.html">SDK</a> for developing JME3 games, you can use any IDE (integrated development environment) such as <a href="../jme3/setting_up_netbeans_and_jme3.html">NetBeans</a> or <a href="../jme3/setting_up_jme3_in_eclipse.html">Eclipse</a>, and even work freely from the command-line. Here is a generic IDE-independent &#8220;getting started&#8221; tutorial.</p></div></div></div>
  4. <div class="sect1"><h2 id="installing-the-jme3-framework">Installing the JME3 Framework</h2><div class="sectionbody"><div class="paragraph"><p>There are many ways to install the JME framework from the command-line, but for this tutorial we will narrow it down to two, the easy way and the hard way.</p></div></div></div>
  5. <div class="sect1"><h2 id="installing-with-git-easy-way">Installing with Git (easy way)</h2><div class="sectionbody"><div class="paragraph"><p>You can build JME3 from the sources. (Recommended for JME3 developers.) This example expects <a href="https://help.github.com/articles/set-up-git/">Git</a> to be installed on your system.</p></div>
  6. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code>mkdir jme3
  7. cd jme3
  8. // if you have a rsa key configured
  9. git clone [email protected]:jMonkeyEngine/jmonkeyengine.git
  10. // or
  11. // if you don't, but you'll be asked for credential after the next command
  12. git clone https://github.com/jMonkeyEngine/jmonkeyengine.git
  13. //can take a while
  14. // build the engine and install it in your local maven repo.
  15. ./gradlew install</code></pre></div></div>
  16. <div class="admonitionblock tip"><table><tr><td class="icon"><i class="fa icon-tip" title="Tip"></i></td><td class="content">What is an <a href="https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/">RSA key</a>?</td></tr></table></div>
  17. <div class="paragraph"><p>The install command will install the built jars into your local maven repositories org.jmonkeyengine folder, which on Linux is ~/.m2, and on Windows might be in AppData, or in your home directory.</p></div>
  18. <div class="paragraph"><p>To use the engine in a game project, you can use <a href="https://hub.jmonkeyengine.org/t/bootmonkey-bootstrap-your-jme-project/37141">BootMonkey</a> or clone a template project from GitHub.</p></div>
  19. <div class="paragraph"><p>To use the template, first create a folder for your project:</p></div>
  20. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code>cd ..
  21. mkdir HelloJME3
  22. cd HelloJME3</code></pre></div></div>
  23. <div class="paragraph"><p>Next, clone one of these templates:</p></div>
  24. <div class="ulist"><ul><li><p><a href="https://github.com/Simsilica/Examples/tree/master/simple-jme">Simsilica/Examples</a></p></li><li><p><a href="https://github.com/Nehon/base-jme">Nehon/base-jme</a></p></li></ul></div>
  25. <div class="paragraph"><p>After cloning, open the <code>build.gradle</code> file and uncomment the <code>mavenLocal()</code> repository, change the version variable, and uncomment any dependencies you will need:</p></div>
  26. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code>repositories {
  27. //This is where jme3 dependencies are stored.
  28. jcenter()
  29. //Uncomment this if you install local dependencies.
  30. mavenLocal()
  31. //Uncomment this if you use external dependencies
  32. //mavenCentral()
  33. //Uncomment this if you use jme3-niftygui
  34. //maven{url 'http://nifty-gui.sourceforge.net/nifty-maven-repo'}
  35. }
  36. ext.jmeVersion = "[3.2,)"
  37. ...
  38. dependencies {
  39. compile "org.jmonkeyengine:jme3-core:$jmeVersion"
  40. compile "org.jmonkeyengine:jme3-desktop:$jmeVersion"
  41. compile "org.jmonkeyengine:jme3-lwjgl:$jmeVersion"
  42. //Those are jme3 additional library uncomment the ones you need
  43. //compile "org.jmonkeyengine:jme3-android-native:$jmeVersion"
  44. //compile "org.jmonkeyengine:jme3-android:$jmeVersion"
  45. //compile "org.jmonkeyengine:jme3-bullet-native-android:$jmeVersion"
  46. //compile "org.jmonkeyengine:jme3-blender:$jmeVersion"
  47. //compile "org.jmonkeyengine:jme3-bullet-native:$jmeVersion"
  48. //compile "org.jmonkeyengine:jme3-bullet:$jmeVersion"
  49. //compile "org.jmonkeyengine:jme3-effects:$jmeVersion"
  50. //compile "org.jmonkeyengine:jme3-jogg:$jmeVersion"
  51. //compile "org.jmonkeyengine:jme3-jogl:$jmeVersion"
  52. //compile "org.jmonkeyengine:jme3-lwjgl3:$jmeVersion"
  53. //compile "org.jmonkeyengine:jme3-networking:$jmeVersion"
  54. //compile "org.jmonkeyengine:jme3-plugins:$jmeVersion"
  55. //compile "org.jmonkeyengine:jme3-terrain:$jmeVersion"
  56. //You need to uncomment nifty repository in the repositories section if you use this dependency
  57. //compile "org.jmonkeyengine:jme3-niftygui:$jmeVersion"
  58. runtime project(':assets')
  59. }
  60. ...</code></pre></div></div>
  61. <div class="paragraph"><p>For a detailed description of the separate jar files see <a href="../jme3/jme3_source_structure.html#structure_of_jmonkeyengine3_jars">this list</a>.</p></div>
  62. <div class="paragraph"><p>That&#8217;s it, you&#8217;re done. After the clone, you will have a project that contains a source package with a default blue cube JME3 app and a subproject that has the proper asset directory structure for your assets.</p></div>
  63. <div class="paragraph"><p>Build and run your project and you should see a blue cube.</p></div></div></div>
  64. <div class="sect1"><h2 id="manual-download-hard-way">Manual Download (hard way)</h2><div class="sectionbody"><div class="paragraph"><p>This example shows how to set up and run a simple application (HelloJME3) that depends on the jMonkeyEngine3 libraries.</p></div>
  65. <div class="paragraph"><p>The directory structure will look as follows:</p></div>
  66. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code>jme3/
  67. jme3/lib
  68. jme3/src
  69. ...
  70. HelloJME3/
  71. HelloJME3/lib
  72. HelloJME3/assets
  73. HelloJME3/src
  74. ...</code></pre></div></div>
  75. <div class="paragraph"><p>To install the development version of jme3, download the latest <a href="https://github.com/jMonkeyEngine/jmonkeyengine/releases">stable release</a>, unzip the folder into a directory named <code>jme3</code>. The filenames here are just an example, but they will always be something like <code>jME3.x-stable</code>.</p></div>
  76. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code>mkdir jme3
  77. cd jme3
  78. unzip jME3.2-stable.zip
  79. cd ..</code></pre></div></div></div></div>
  80. <div class="sect1"><h2 id="sample-project-directory-structure">Sample Project Directory Structure</h2><div class="sectionbody"><div class="paragraph"><p>First we set up the directory and source package structure for your game project. Note that the game project directory <code>HelloJME3</code> is on the same level as your <code>jme3</code> folder. In this example, we create a Java package that we call <code>mygame</code> in the source directory.</p></div>
  81. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code>mkdir HelloJME3
  82. mkdir HelloJME3/src
  83. mkdir HelloJME3/src/mygame</code></pre></div></div></div></div>
  84. <div class="sect2"><h3 id="libraries">Libraries</h3><div class="paragraph"><p>Next you copy the necessary JAR libraries from the download to your project. You only have to do this set of steps once every time you download a new JME3 build. For a detailed description of the separate jar files see <a href="../jme3/jme3_source_structure.html#structure_of_jmonkeyengine3_jars">this list</a>.</p></div>
  85. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code>mkdir HelloJME3/build
  86. mkdir HelloJME3/lib
  87. cp jme3/lib/*.* HelloJME3/lib</code></pre></div></div>
  88. <div class="sect2"><h3 id="sample-code">Sample Code</h3><div class="paragraph"><p>To test your setup, create the file <code>HelloJME3/src/mygame/HelloJME3.java</code> with any text editor, paste the following sample code, and save.</p></div>
  89. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">package</span> <span class="namespace">mygame</span>;
  90. <span class="keyword">import</span> <span class="include">com.jme3.app.SimpleApplication</span>;
  91. <span class="keyword">import</span> <span class="include">com.jme3.material.Material</span>;
  92. <span class="keyword">import</span> <span class="include">com.jme3.math.Vector3f</span>;
  93. <span class="keyword">import</span> <span class="include">com.jme3.scene.Geometry</span>;
  94. <span class="keyword">import</span> <span class="include">com.jme3.scene.shape.Box</span>;
  95. <span class="keyword">import</span> <span class="include">com.jme3.math.ColorRGBA</span>;
  96. <span class="directive">public</span> <span class="type">class</span> <span class="class">HelloJME3</span> <span class="directive">extends</span> SimpleApplication {
  97. <span class="directive">public</span> <span class="directive">static</span> <span class="type">void</span> main(<span class="predefined-type">String</span><span class="type">[]</span> args){
  98. HelloJME3 app = <span class="keyword">new</span> HelloJME3();
  99. app.start();
  100. }
  101. <span class="annotation">@Override</span>
  102. <span class="directive">public</span> <span class="type">void</span> simpleInitApp() {
  103. <span class="predefined-type">Box</span> b = <span class="keyword">new</span> <span class="predefined-type">Box</span>(Vector3f.ZERO, <span class="integer">1</span>, <span class="integer">1</span>, <span class="integer">1</span>);
  104. Geometry geom = <span class="keyword">new</span> Geometry(<span class="string"><span class="delimiter">&quot;</span><span class="content">Box</span><span class="delimiter">&quot;</span></span>, b);
  105. Material mat = <span class="keyword">new</span> Material(assetManager,
  106. <span class="string"><span class="delimiter">&quot;</span><span class="content">Common/MatDefs/Misc/Unshaded.j3md</span><span class="delimiter">&quot;</span></span>);
  107. mat.setColor(<span class="string"><span class="delimiter">&quot;</span><span class="content">Color</span><span class="delimiter">&quot;</span></span>, ColorRGBA.Blue);
  108. geom.setMaterial(mat);
  109. rootNode.attachChild(geom);
  110. }
  111. }</code></pre></div></div></div></div>
  112. <div class="sect1"><h2 id="build-and-run">Build and Run</h2><div class="sectionbody"><div class="paragraph"><p>We build the sample application into the build directory…</p></div>
  113. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code>cd HelloJME3
  114. javac -d build -cp "lib/eventbus.jar:lib/j-ogg-all.jar:lib/jme3-lwjgl.jar:lib/jme3-bullet.jar:lib/jinput.jar:lib/lwjgl.jar:lib/stack-alloc.jar:lib/vecmath.jar:lib/xpp3.jar:lib/jme3-blender.jar:lib/jme3-core.jar:lib/jme3-desktop.jar:lib/jme3-jogg.jar:lib/jme3-plugins.jar:lib/jme3-terrain.jar:lib/jme3-testdata.jar:lib/jme3-niftygui.jar:lib/nifty-default-controls.jar:lib/nifty-examples.jar:lib/nifty-style-black.jar:lib/nifty.jar:." src/mygame/HelloJME3.java</code></pre></div></div>
  115. <div class="paragraph"><p>… and run it.</p></div>
  116. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code>cd build
  117. java -cp "../lib/eventbus.jar:../lib/j-ogg-all.jar:../lib/jme3-lwjgl.jar:../lib/jme3-bullet.jar:../lib/jinput.jar:../lib/lwjgl.jar:../lib/stack-alloc.jar:../lib/vecmath.jar:../lib/xpp3.jar:../lib/jme3-blender.jar:../lib/jme3-core.jar:../lib/jme3-desktop.jar:../lib/jme3-jogg.jar:../lib/jme3-plugins.jar:../lib/jme3-terrain.jar:../lib/jme3-testdata.jar:../lib/jme3-niftygui.jar:../lib/nifty-default-controls.jar:../lib/nifty-examples.jar:../lib/nifty-style-black.jar:../lib/nifty.jar:." mygame/HelloJME3</code></pre></div></div>
  118. <div class="admonitionblock note"><table><tr><td class="icon"><i class="fa icon-note" title="Note"></i></td><td class="content">If you use Windows, the classpath separator is <kbd>\</kbd> instead of <kbd>/</kbd>.</td></tr></table></div>
  119. <div class="paragraph"><p>If a settings dialog pops up, confirm the default settings. You should now see a simple window with a 3-D cube. Use the mouse and the WASD keys to move. It works!</p></div></div></div>
  120. <div class="sect1"><h2 id="recommended-asset-directory-structure">Recommended Asset Directory Structure</h2><div class="sectionbody"><div class="paragraph"><p>For <a href="../jme3/intermediate/multi-media_asset_pipeline.html">multi-media files, models, and other assets</a>, we recommend creating the following project structure:</p></div>
  121. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code>cd HelloJME3
  122. mkdir assets
  123. mkdir assets/Interface
  124. mkdir assets/Materials
  125. mkdir assets/MatDefs
  126. mkdir assets/Models
  127. mkdir assets/Scenes
  128. mkdir assets/Shaders
  129. mkdir assets/Sounds
  130. mkdir assets/Textures</code></pre></div></div>
  131. <div class="paragraph"><p>This directory structure will allow <a href="../jme3/intermediate/simpleapplication.html">SimpleApplication</a>'s default <a href="../jme3/advanced/asset_manager.html">AssetManager</a> to load media files from your <code>assets</code> directory, like in this example:</p></div>
  132. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code>import com.jme3.scene.Spatial;
  133. ...
  134. Spatial elephant = assetManager.loadModel("Models/Elephant/Elephant.meshxml");
  135. rootNode.attachChild(elephant);
  136. ...</code></pre></div></div>
  137. <div class="paragraph"><p>You will learn more about the asset manager and how to customize it later. For now feel free to structure your assets (images, textures, models) into further sub-directories, like in this example the <code>assets/models/Elephant</code> directory that contains the <code>elephant.mesh.xml</code> model and its materials.</p></div></div></div>
  138. <div class="sect1"><h2 id="next-steps">Next Steps</h2><div class="sectionbody"><div class="paragraph"><p>Now follow the <a href="../jme3.html">tutorials</a> and write your first jMonkeyEngine game.</p></div></div></div></div><div id="footer"><div id="footer-text">Version <br>Last updated 2019-12-20 23:30:51 +00:00</div></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script><script>docsearch({
  139. apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
  140. indexName: 'jmonkeyengine',
  141. inputSelector: '#doc-search',
  142. debug: false // Set debug to true if you want to inspect the dropdown
  143. });</script></body></html>