Showing posts with label ubuntu. Show all posts
Showing posts with label ubuntu. Show all posts

Saturday, January 5, 2013

Java Programming Quickstart in Ubuntu 12.04


I simply love working in Ruby and Python. It's easy to quickly write code in pretty much any dynamically-typed language. Yet, I feel a little nostalgic when I see programs written in C++ or Java. So, I'm trying to spend a little bit more of my time exploring object oriented programming in these statically-type languages.

At times, I forget how to get started writing, compiling, and running Java programs. So, here's a little reminder of how to do it.

Install Java

Feel free to install any version of the Java JDK. I'll be using OpenJDK:
sudo apt-get install openjdk-6-jdk

Write Some Java Code

We'll start by writing a simple Hello World program in a file named HelloWorld.java:
class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
}
Be sure to name the file HelloWorld.java! The Java compiler requires the name of the file to be the same as the name of the clas. By following this convention, the compiler will automatically handle how classes in separate files reference each other. Also, each file can only have one public class. It makes sense to name the file after your public class so it can easily be referenced. More information can be found in this PDF.

The Java interpreter starts by executing a class' main() function. So, we define a static function named main in our HelloWorld class. The interpreter will execute our code and print "Hello World!" to the screen.

Compile and Run the Code

In the terminal, we compile the code using the javac command:
javac HelloWorld.java
This turns our Java code into bytecode the interpreter can understand. A new file named HelloWorld.class will be created in the current directory.

Now, we're finally ready to run the code! Just tell the Java interpreter to load our compiled bytecode:
java HelloWorld

> Hello World!
Voila! I'm sure most Java programs will be far more complex than this. So, I recommend using a full-fledged IDE like Netbeans or Eclipse. For people who are just learning the Java language, this is a great way to get started!

Wednesday, October 10, 2012

Basic Git Prettification (Colors and Shortcuts) in Linux

At work, we use Git as our version control system. When I installed Git on OSX via Homebrew, it already had some useful configs setup out of the box. I love Git so much, I now host any small coding projects on Github (not much going on right now).

I installed Git on Ubuntu 12.04 using apt-get, but it failed to configure all the nice little shortcuts and color schemes I've gotten used to in OSX. Here are some useful git configs I just can't seem to do without. You can edit your .gitconfig or run these commands directly in your terminal:

Status/Diff Colors
git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto
Command Shortcuts
git config --global alias.br branch
git config --global alias.co checkout
git config --global alias.st status
User Info
git config --global user.name "Your Name Comes Here"
git config --global user.email you@yourdomain.example.com
Let me know what other useful settings you have in your Git configuration!

Monday, August 27, 2012

Command Line Pandora with Pianobar


I really enjoy living as much of my life in the CLI as possible. It takes up less memory and is arguably easier to use. For streaming music, I like to use Pandora radio. After searching around, I found a cool command line Pandora client called Pianobar.

As of Ubuntu 12.04 and the writing of this blog, the repository version of Pianobar doesn't work since Pandora has changed around its API. So, we have to compile it from source.

Install Dependencies

For Ubuntu, run the following in the terminal:
sudo apt-get install libao-dev libfaad-dev libmad0-dev libjson0 libjson0-dev libgnutls-dev
For OSX, install Homebrew and run:
brew install libao faad2 libmad json-c gnutls

Download and Compile Pianobar

The latest version of Pianobar as of the writing of this blog is version 2012.06.24. Save the file anywhere on your computer and navigate to that directory. You'll need to uncompress the file by running the following:
tar xvjf pianobar-2012.06.24.tar.bz2
Now, we are ready to compile. Navigate into the uncompressed directory and run:
make clean
make
This will create an executable pianobar. You can test it by running:
./pianobar

Install Pianobar

If everything works correctly, run:
make install
Now enjoy Pandora from the command line!