Saturday, April 13, 2013

Creating A JAR File From Scratch on a Mac in 4 Convoluted Steps

Skip this introductory blurb for steps!

A common problem in engineering that (race card, activate!) hinders minorities from immediately accessing a culture of engineering is the practice of searching. Yes, really. Back in college, a black buddy and I went to our CSCI professor's office hours to ask about the class content.

He told us to go to google.com and typed in exactly what we asked him.

She was livid and we were both pretty insulted. It was pretty lame, but I understand his mindset, today: there are lots of people that grew up with everything they needed to learn how to build stuff. Experienced adults and professionals and their recommended resources are great supplements for learning industry-level creation. Those people know how to search for the right terms that put them down the right road to get a solution.

Today, when I go to search for how to create a .jar file from scratch, a Google search yields parts of processes that assume I've set up my environment with Eclipse or Ant, that I know how to run a command from the terminal, that I know the terminology used at Oracle's documentation, or some other thing a cool programmer might have shown me or berated me about on IRC. I do know these things, but my younger compadres in El Monte that haven't gone to USC might not, if they were anything like me. To give an end-to-end depiction, I'm listing my discoveries below.

Disclosure: the above also means that I don't know how to do everything I'm outlining below. I'm going to figure it out and you're coming with me. Your mileage may vary, so don't blame me if it doesn't work. At the end, it'll have worked for me, pal!Rudeness disclosure: if you're an experienced programmer, some of my language will offend you because it's going to sound so lazy. That's okay. I really just want what you do: good practices and good technology. Unfortunately, I'm really critical of how things have ended up, so I'm going to sound like an annoying UX designer and point out every single inconvenience as if it's something an ignorant, uncaring jerk would have implemented. You have been warned. What's that you say? Why don't I change it myself? Because, somehow, we've screwed ourselves even at a meta level. It's better to at least berate implementations that came before me about these problems so the next generations make things incrementally better, for now, until I start a company to improve engineering accessibility.

Steps: How to Create A .JAR from Scratch (On An Apple (sucks) Mac)

1. I have the JDK installed. Maybe you should, too. Here's how to get it.

Go to the Java people's website. Their name is Oracle. You see a digital ton of scary buttons, images, and links.

Right now, the link I used says "Java Platform (JDK) 7u17" under it. Click that image (it's actually a button).

Install it like you would any other app. Usually that means you double click the file after you download it.

2. Get Eclipse. It's like Microsoft Word but for code instead of essays.

Go to Google's Android website and click the button to download the bundle. You can always Google the words "Android SDK" to find this sort of thing.

It's a nifty folder with two folders inside that say "eclipse" and another that says "sdk". Go into the "eclipse" folder and you'll find an executable application called "Eclipse". Double-click on that to run it.

Some popup will come up. The popup is dumb and useless to me, but I never check the box that lets me avoid it in the future because I'm paranoid. What if, you know? What IF I want to change where my working files go because it's the end of the world unless I can change my working directory? Just click the confirmation button that you're cool with it (OK or YES or whatever other submission of will is available) and feel free to be less paranoid than I.

3. Set up your code that will become your JAR file. This is like opening a new Document in Microsoft Word, but with a lot more convoluted steps that you need to do before you can actually get anything in a practical state to work.

With Eclipse open, go to the File > New > Java Project menu items.

Next, fill out the minimal information like I did in the screenshots below, because too many programmers are still so abysmally ignorant with actual UX in our own tools that you'd think we're simultaneously sadistic and masochistic with all the goddamn text fields, radio buttons, and drop-downs we force on ourselves. Here, I wrote "Making A Jar" as the project name.

Click Finish. I was busy taking screenshots, so make sure to fill in any information I might have forgotten to let you click it.

Man, that is one sweet project. Sit back and nod to yourself confidently, with a deserved smugness.

What's that? Oh, you savvy cookie, you're absolutely right: we need a class up in this project so that you can actually write the code for your JAR file. After all, this class is going to be what your JAR file contains and it's what other programs could talk with. Expand some folders in the project pane (there are folder icons listed in it) until you see the "src" folder. Then right click on that and select New > Class.

It's almost as if you did something wrong just by opening a freakin' window. You can get rid of that error message and its high-and-mighty caution sign by filling in the Package field with something in a format that you wouldn't know about unless someone told you (nothing like some pain for the newbies to give you that frat initiation feeling, COM.WELCOME.TO.PROGRAMMING.NEWBIE.). Fill it in with something like "com.yourpackagenamehere". As you can see from the screenshot below, I filled it out with "com.shoulddoitforme", because it should. Also, a class can have a main(...) method in it, usually if it's the entry point for your application. Don't know what that means? I clicked it, but don't worry about it. At least now you can click the Finish button without a warning in yo' face.

Congratulations, you now have a Java class. This'll contain the function that you can call from something using your .JAR file, supposedly.

Now you can write in whatever awesome functionality your JAR file will contain. At least, that's what I think you can do. I've never done this before, so... let's find out! Here's a method that inquires about your well-being. You can write it too, I guess, and hope it works later.

Press CMD-S to save and let's see if we can create a JAR out of this. New territory for me here, too, folks!

4. Make the JAR file!

Don't click it, but some link with a pretty good list but a lot of words (hey, I know I'm not the most curt communicator in this posting; it's "style") tells you how to make the JAR file in Eclipse. Here's the visual version: start with File > Export.

An interface that asks you about a destination appears. Ignore the words on it, it actually just wants you to select Java > JAR file. The actual "destination" is later, so click Next.

I don't actually know what selections to make with regards to the export, but you can see what I chose below. The JAR file location has to be specified. I also checked "Export Java source files and resources", though I'm not sure how that's different than a JAR that doesn't do so. Specify the destination and click Finish.

Congratulations! You made a JAR file from scratch in 4 easy convoluted steps!

The reason I'm discovering this is because I need to get a JAR into Unity. I don't know how to do that with ours, so creating a JAR from scratch -- where I know everything about its creation -- seems like a good start. Now I can use this JAR against the instructions for getting it into Unity, which involves all sorts of fun JNI nonsense, and then repeat that process with the "real" JAR file that I want to use in Unity.

That's how you make a JAR, folks, but I don't know how good it'll be for practical usage. Will it work with my Unity case? I'll find out! Do you already know? Let other people know in the comments and chime in on whether or not something is wrong with the steps here!