Stargrasper's Java Basics Tutorial SeriesYou know how I promised the next tutorial would teach how to write code? I lied. I decided in the intervening time that it would be wise to give a tutorial on basic use of
Eclipse,
Netbeans, and simply not using an IDE to code. Normally, I would be doing all of my coding on Linux. For purposes of screenshots, I've decided to switch to Windows because the small differences that do exist are probably confusing. If you have OS related questions, don't hesitate to ask. I've done Java coding on every major OS in use today.
Now, I'm going to try to hold your hand as tightly as I can while walking you through this.
Eclipse BasicsThe current version of Eclipse is 3.7.1. I've been using Eclipse 4.2 Beta. They look different. That means I'm dropping down for this tutorial.
I'm going to assume you can handle downloading and unpacking Eclipse. When you launch it, the first thing it will do is ask you for a workspace. This is your working directory, a folder where it will keep all of your stuff. I believe it will create the directory if you hand it something that doesn't exist. I think. It looks like this.
After choosing your workspace, if you've never used that workspace before, Eclipse will bring you to its start screen. Depending on what plugins are installed, it will look something like this.
Click on 'Workbench' in the top right corner.
Which brings you to this screen. It's pretty blank now because no projects have been created.
On the left is the
Package Explorer. Projects and the associated file trees will appear in here. To the right is the
Outline. It's nice for figuring out what's in your code, but I usually close it to give me more room for the code. The bottom is a series of tabs. The only two you really care about are
Problems, which describes errors in your code, and
Console, which is where both stdout and errout goes by default in Eclipse. The big open space in the center is where the code editor will go once we create a code file. So lets do that!
Select the
File menu,
New,
Java Project. Go ahead and enter "Hello World" as the project name and press finish.
I'm using Java 7 because it's the most recent version of Java. You may only have Java 6. It's not significant for anything we'll be doing for awhile.
Open
Hello World in the
Package Explorer and click on the
src folder. Now press
New Package in the center of the tool bar or from the
File / New menu.
Enter a name and press Finish to create your package.
Click on your package in the
Package Explorer and click
New Class from either the center of the tool bar (next to
New Package) or from the
File / New menu.
Enter a name and check the box that says
public static void main(String[] args).
The code editor for your new file will appear in the center of the screen. Write some code, in my example, the classic
Hello World program, and press the
Run button as highlighted or press
Ctrl + F11 to run your code.
Eclipse will open the
Console tab and display the output, which may include errors or exceptions if something went wrong. If the code tries to loop infinitely or something crazy or if you just need to kill the program
now, there's a little red button near the bottom right that will kill the running program immediately.
Great! You know enough about eclipse to get started coding. But what if you want to share your code with someone else? What if you want to collaborate? What if you want help debugging? What if you want to distribute the working executable without sharing the code? All except the last
could be done by sharing code excerpts like we've been doing elsewhere in this thread. But that's not very efficient. The more efficient way is to distribute a .jar file. A .jar file is literally a .zip file that Sun uses for specialized purposes. You can make .jar files executable, which means they can run the program contained within them. You can also choose whether or not to include the source code. A program can run just fine without .java files using only the compiled .class files.
To create a .jar file, right click the project name in the
Package Explorer and select
Export. Select
Java and then either
Jar File or
Runnable Jar File. A Jar file contains just data. A Runnable Jar File can be executed to run the program inside. From there, you need to select the export location and name of the jar file as well as check what you want included. The class files are needed to run without recompiling. The source may or may not be desirable to include.
So you know how to export a Jar file. Fantastic! How do you import it?
File / Import / General / Archive File. Make sure you've got a project to plug it into.
Eclipse has a metric crap load of other functions and features I'm not talking about here. This is just the very basics to get you started.
Netbeans BasicsI will abridge this one heavily because Netbeans is similar enough to Eclipse that there is a lot of overlap and because I don't have time to write a massive tutorial with screenshots right now. That and I don't use Netbeans often and am thus unfamiliar with its intricacies.
I have no idea what the most recent version of Netbeans is and I don't feel like checking. I happen to have Netbeans 6.8 on my computer from heaven knows when I installed it.
Creating projects in Netbeans is about the same as Eclipse.
File / New Project / Java / Java Application. Give it a name, location, and check the box that makes it generate a main() method for you.
The handy thing is that this just created your package and your class file for you. That being said, to create your own, you select
File / New File and pick either package, class, or whatever from the list.
You can run the program by using the
Run menu or the
Run button.
Netbeans actually builds Jar files for you every time you build the program. You'll find it under the
Files tab.
Importing Jar files in unintuitive as near as I can tell. Follow
these instructions to do it.
I feel like I'm snubbing Netbeans, but frankly, I don't really know much about using it and I don't have the time to make as many screenshots as I did for Eclipse. You'll figure it out and if you don't, you'll ask me and I'll create all the screenshots and videos you want to walk you through it. Don't be afraid to ask.
Command Line BasicsRemember how I said I'm a Linux user? It's not uncommon for me to do things on the terminal. To do it in Windows, you use one of the following commands:
java class
or
java -jar jarFileName
There's no particular reason you need an IDE to code in Java. You can write your code in any text editor. VI or EMACS might be traditional. There's nothing stopping you from using Notepad, though Notepad++ might be more helpful just for syntax highlighting. If you know Sun's Java specifications, you can produce Java in any environment.
Building Jar files is harder.
jar cf jar-file input-file(s)
I ripped that directly from an Oracle help page. I won't go into detail here because nobody reading this is going to try doing it.
http://java.sun.com/developer/Books/javaprogramming/JAR/basics/build.htmlPost EpilogueThat took forever to write. And since nobody has problems setting up their IDEs, nobody will bother to read this. Oh well. We can point people to this thread to help get them started.
I'd strongly appreciate any and all comments. I'd certainly like to know what I'm doing well and poorly writing these things. I'm happy to clarify anything and answer random questions as well.
I'd better get started on a tutorial that teaches coding before someone lynches me.