Gdx.files.internal won't work (blueJ) - java

I am trying to make the HelloWorldImage example from Beginning Java Development with Libgdx but I can't get the project to read the image.
This is what the project directory and first class looks like:
And this is the error I get when I try to run the program:

I also used "Beginning Java Game Development with LibGDX" to learn LibGDX, so I know you are using BlueJ because that's what the book does. You definitely can build LibGDX games with BlueJ: the author uses it to develop dozens of games in the book. That said, the other commenters are correct: BlueJ is an "educational IDE" arguably suitable for learning Java. You could consider using a different IDE, especially if you already know Java.
The book's source code is available for download on GitHub. Download and extract the "978-1-4842-1501-2_Ch01_code.zip" archive and see how the project files are supposed to be arranged. If you compare your project to the source, it looks like you're missing the "+libs" folder which should contain gdx.jar, gdx-backend-lwjgl.jar, gdx-backend-lwjgl-natives.jar, and gdx-natives.jar. I know the author described several ways these dependencies could be installed, so perhaps you have used an alternative method, but I recommend you use the structure you find on GitHub to ensure it's working.
(If you want to use a different IDE like Netbeans or Eclipse but don't want to use LibGDX's Gradle build tool, I wrote a short Maven pom file that can be used to build the LibGDX games in the book.)

You don't have libGDX's dependencies (i.e. libGDX libraries etc.) included in your program's classpath.
I don't think BlueJ even has an option to use dependencies. Use Eclipse, NetBeans or IntelliJ, the only 3 officially supported IDEs for libGDX (and the most commonly used).
Edit: You can add your libraries in a folder called +libs in the BlueJ project directory.
I would not recommend using BlueJ - BlueJ is, IMO, often used for teaching Java concepts (because of its object visualizer etc.) but has an extremely limited feature set.
Also, BlueJ Gradle integration doesn't exist (a useful tool for development in general.)
Instructions for setting up libGDX with these 3 IDEs are available here.

Related

Can't create Netbeans JAR files

I’m a novice programmer and I bit off more than I could chew. I’m using Netbeans 8.2 for the first time and I managed to hobble it together and install the Python 3.0 plugin followed by the Jython 2.7 plugin (never used either of these before.) I’m a model railroader and use a program call JMRI (but not its JAR file in my code) which is written in Java and my code is for trains so I chose Jython so I could share it with others.
I have a good working program! Now I want to create JAR files so I can share it. Clean & build does nothing, I click it and the output window remains blank. I right click the project and see Clean and make egg. Researching the internet I find that I’m not the only one that has struggled with making Jython JAR files. But the Q& A are dated (2011-2014) and refer to programs JUMP and JthonC which appear to be unavailable now.
I’m looking for tutorials and examples and of course experienced users to guide me. What program (or plugin) do I need to make the JAR file?

Clarification for exporting a LibGDX game as an applet

I'm trying to follow the guide to export my project as a Java applet.
My project, as seen in Eclipse, has three directories:
Game
Game-Android
Game-Desktop
They were created using the UI Setup tool. The guide at http://www.thesecretpie.com/2011/05/being-like-minecraft-or-how-to-run-your.html is confusing and I don't understand these points:
Next step: add a new file called HelloWorldApplet.java (in the same
directory as other source files).
"The same directory as other source files". Well, I have three directories and they all have a source folder. In which of these three am I supposed to put this file? I tried placing it in Game, but Eclipse is unable to resolve com.badlogic.gdx.backends.lwjgl.LwjglApplet. If I put it in Game-Desktop it seems to be OK, but I'm not sure.
Create a directory called applet in your project dir.
Again, in which of my three directories?
Just export the whole project as jar in Eclipse
I can export a JAR for Game, Game-Android, or Game-Desktop. Which of these?
Related:
I happen to be using the JRuby JAR in my game to run some ruby scripting logic. Do I have to do something specific about it?
That blog is little old. For example, the JOGL backend is gone from recent version of Libgdx (so you don't need to worry about switching to the LWJGL backend). Also, the current LWJGL bundled into Libgdx is 2.9.0 (hopefully that doesn't change too much, though).
I believe the instructions are effectively packing up the "desktop" build of your app into an Applet (as the desktop is the only build that use the LWJGL backend), so assume Game-Desktop is the "same directory" that its talking about.
Thus, you should export the .JAR from your Desktop project.
On a related note, there is another way to run your game on the web, using the GWT backend. Check out the Setup & Running page (where its called "HTML5"). There are a bunch of caveats on using the GWT backend (not all Libgdx APIs work over there). (Hmm... I doubt this will work with the JRuby JAR, so probably not worth spending too much time on that.)

Clojure and Java interop in a real world

I'm thinking about start using (not playing with) Clojure. Are there any useful guides? I'm not asking about lein, javac or any other 'small' manual tools. I need to know how to have Java and Clojure sources in eclipse in the same project. How to make them calling each other without compilation errors? How to configure maven? How to set up fully productive development environment? Is it even possible at the moment? What plugins may be useful? Where to start?
I have a fully working production setup with Eclipse, Maven and Clojure that is working very nicely at the moment. Hopefully it is helpful as an example of a good polyglot setup within a Java IDE.
I don't use leiningen - Nothing against lein at all - it's very nice and ideal in a pure Clojure/CLI world. But I've found that pure Maven is nicer to work with in a polyglot Java+Clojure environment with an IDE since the tool integration is so much better. Also from an ecosystem / audience / community perspective if you want people from the Java world to be able to build your source you are going to cause a lot less confusion if you just use Maven directly.
Here is my setup:
Eclipse 4.2 as main IDE
Counterclockwise Eclipse plugin - very good, takes care of REPL, Clojure editing etc.
Maven used to manage all projects (I use the built in Eclipse Maven integration mostly, but occasionally use the CLI version as well)
cljunit used to enable JUnit tests to run on Clojure parts of the project
Github / Travis CI used for SCM and Continuous integration, accessed using the built-in EGit team provider in Eclipse
In terms of actually how I manage / set up the project itself:
I configure everything with Maven, using standard Maven directory layout. Polyglot Java+Clojure Projects typically have both src/main/java and src/main/clojure
Clojure is just a Maven dependency, like any other Java library.
I make the Clojure source directories into resource directories in the Maven setup. This means that the .clj files get bundled in any jars and can be loaded / run dynamically at runtime.
I usually make the entry point on the Java side with a public static void main(...) as usual, but call quite quickly into the Clojure code. See this blog post on calling Clojure from Java.
Finally some coding tips for polyglot Java+Clojure
I find that Java is better for low level data structures, libraries and algorithms, while Clojure is better for integrating things together and "glue" code.
Clojure calling Java is usually easier / more elegant than the other way round. Also it makes sense since you generally want the dependencies to flow that way (higher level code calling lower level code)
If you make all your Java classes immutable, they play very nicely in a Clojure world with minimal effort.
Sometimes it is worth making one or more of your Java classes implement some of the Clojure interfaces, particularly clojure.lang.IFn for example. This way your Java objects can act as first class functions in Clojure code.
Here's an example project that mixes Java and Clojure source:
https://github.com/mikera/enlight
I also wrote a small library (clojure-utils) that includes some example code for calling Clojure from Java, which you may find useful.
Despite your tone about leiningen, I recommend looking at it. Leiningen supports Java compilation, so combining java and clojure sources in one project isn't a problem.
The Counterclockwise plugin, the clojure plugin for Eclipse, can work with leiningen project files (project.clj). So within Eclipse you have dependency management and java compilation all handled for you by defining the right things in project.clj, without the need to install leiningen separately or execute commands from the command line.
In the project.clj set :java-source-paths, like for example:
:java-source-paths ["src/main/java"]
In package src/main/java put a class Foo:
package main.java;
public class Foo {
public static final String value = "Michiel";
}
Somewhere in a clojure source file define this function and "Michiel" will be printed when it is called:
(defn foo
"I don't do a whole lot."
[]
(println (main.Foo/value)))
Further reading:
http://leiningen.org/
http://code.google.com/p/counterclockwise/
You could also try the framework "Funky". It will completly seperate you Clojure and Java code . Just have a look at https://github.com/windler/Funky

Beginner Java Developer Netbeans/Eclipse IDE Questions

Im working on building a java application in windows vista using the Emotiv Epoc Neuroheadset, http://emotiv.com/index.php.
I'm new to java and having a really tough time getting started. I have some examples that came with the headset I'm trying to run in either eclipse or netbeans (at this point I really don't have a preference, suggestions?)
The user manual gives these directions:
*2. Package content: Jna.rar, Edk.java, EmoState.java, EDkErrorCode.java
Example 1: EEGLog
Example 2: EmoStateLog
Developer‘s guide: Step 1: Add the JNA library to your project. Step 2: Add Edk.java, EmoState.java, EdkErrorCode.java to your project. The classes in these files contain methods calling the APIs from Edk.dll using JNA library. You can find more information about the conversion between data types on the JNA's homepage.*
This is all Chinese to me! Can someone explain to me exactly how to do these steps (in a way a small child who have never used eclipse or netbeans before can understand!) Somebody from the Emotiv Epoc forum gave me these directions as well:
*To see how to Java wrapper examples work you should install Eclipse and open, compile them. You put edk_utils.dll and edk.dll into System32 or the same root directory of each example as EmoStateLog, CognitivExample,... before running them.*
My problem is I dont know enough about the IDEs to run these examples. Am I supposed to make a new project then add these files? Or do I open one of the examples, then add the .dll files and the JNA library? How do you add libraries to a project? Where do I put the .dll files?
Thanks in advance!
I know this is fairly simple stuff but its always the simple stuff thats hardest to figure out it seems!
-Eric
Based purely off of what you're saying here...
Developer‘s guide: Step 1: Add the JNA library to your project. Step 2: Add Edk.java, EmoState.java, EdkErrorCode.java to your project. The classes in these files contain methods calling the APIs from Edk.dll using JNA library. You can find more information about the conversion between data types on the JNA's homepage.*
Create a new project in Eclipse.
Add Edk.java, Emostate.java, EdkErrorCode.java into the src folder
Add the JNA library into the libs folder. (Make sure the library is in the project path, otherwise Eclipse won't detect it.)
Whether or not it will compile depends on if those are the only supporting files.

How is a Java Project Structured (Compared to a Visual Studio C# Project)?

I'm trying to learn project automation and I'm using the book Pragmatic Project Automation as a guide. It's examples are in Java so I find it easier to follow it along in Java. I don't have any experience using Java or any of its IDEs. However, I've learned some C# using Visual Studio (although I'm still a beginner).
I'm having trouble understanding some parts of setting up a Java project. I'm using Netbeans IDE 7.0 on Windows 7. With Visual Studio, I have a solution with projects underneath. With Netbeans, I appear to just have a project with directories determining the rest of the structure (and the IDE to some degree)? The equivalent to Add Reference appears to be adding a source to the Classpath. There also seems to be a degree of separation between the compiler and the IDE. I'm currently in a situation where I can compile my project just fine while the IDE tells me I still have errors (and I assume this is because I have the project set up incorrectly).
I'm basically looking for analogies that will help me better understand Java project structure.
Lots of similarities between the two languages and IDEs. I spent many years in both. For starters the equivalent to 'add reference' in VS is adding a library or jar in netbeans. In respect to a reference - a jar is pretty much the same thing as a module or .dll in VS. A jar is a compiled reference. To add a reference just go to the project menu and then properties then to the libraries menu from there you can add either pre-assembled netbeans libraries, which are collections of .jar's, or a single .jar, or even a project. Once you add a reference you can import them into your class just like you would in C#.
Netbeans doesn't really have a 'solution' as VS does. You deal with individual projects. It does however have the capability to add a project as a reference so you don't have to continually re-build the references when you change something between multiple projects. It also has project groups to group similar projects.
Lastly Apache ANT is responsible for tying everything together in the background. Netbeans creates a build.xml and build-impl.xml file in the background to tell ANT how to assemble the project into a .jar.
There are my other things to cover but I thing this answers most of your questions. Does this help?
I can't speak for NetBeans, as I use Eclipse, but you are on the right track with classpath being roughly equivalent to references in the Visual Studio world. Libraries (usually .jar files) are placed on the classpath and must be there both at compile time and runtime (you specify the classpath to the compiler at compile time, and to the JVM at runtime). The classpath can contain many different entries, and they can be anywhere in the project structure (or outside of it entirely).
Java itself doesn't impose many restrictions on your project structure, although various IDEs and build tools do. The one thing that is a universal restriction in all Java environments is that source files (and class files) are placed in a directory named after the package name. So if your package name is com.test.something, then your source files will be in SRC_DIR/com/test/something, and your class files in OUT_DIR/com/test/something (note: SRC_DIR and OUT_DIR are not special variables; each IDE will have a different way to specify those directories).
Java libraires tend to heavily build-on one-another, so at some point, you'll find that the classpath entries are too many to manage manually. Before you get there, you'll want to take a look at Apache Maven or Apache Ivy which are dependency management tools. You'll need to understand how they work (either one, not both) and how to integrate them with your IDE. If you use Eclipse and Maven, m2eclipse offers fairly complete integration between the IDE and the dependency management tool.
With Netbeans, I appear to just have a
project with directories determining
the rest of the structure (and the IDE
to some degree)?
Visual Studio dictates a particular project layout and since the compiler is so tightly integrated into the IDE there's no real concept of a build script. In contrast, Java has no such structure (although certain 'best practices' have emerged such as having a 'src' directory for source files, 'lib' for libraries, 'test' for test source, etc.) and a build script is usually required to tell the compiler were to find source files and libraries, what artefacts to produce and a miscellany of other chores (running tests, deployment, creating code metrics and so forth).
In simple cases, the IDE will take care of this for you (if you follow the convention for that particular IDE) but ultimately you will probably want to take a look at a build tool to understand what's going on behind the scenes. Apache Ant and Apache Maven are both prominent offerings. Ant is very flexible whereas Maven attempts to dictate a common layout. I suggest you investigate both and see which suits.
There also seems to be a degree of
separation between the compiler and
the IDE. I'm currently in a situation
where I can compile my project just
fine while the IDE tells me I still
have errors
If your code compiles, it is correct. The IDE is simply acting in an advisory capacity (and will highlight issues beyond compiler errors, such as warning you of potential code mistakes or bad practice).
and I assume this is because I have
the project set up incorrectly
This is a possibility although, as stated above, there are many other explanations.

Categories

Resources