Ben Writes!

  • Home
  • Resume
  • About

29

Dec

What is Maven? A Beginner’s Tutorial by Example

Posted by Ben  Published in Software Development, Technical Writings

Today, I published a beginner’s tutorial to Maven using Amazon’s ‘Kindle Direct Publishing’.

In my tutorial, I cover:

  • generating a Maven project
  • scripting Maven commands
  • adding dependencies to a Maven project
  • packaging dependencies into a Maven project
  • installing Maven projects into the local repository
  • installing non-Maven projects (using third party .jar files) into the local repository.

And if you get stuck on any of the commands, code, or XML examples, you can view the projects’ source code and files online. You can check out my tutorial here! Thanks! :D

no comment

5

Apr

JSON code example in Java

Posted by Ben  Published in Software Development

Today I discovered that Android’s serializable interface is not the same as Java’s serializable interface, and they are not really compatible. The solution? Well, there are quite a few possible solutions, but one of the easier/lightweight/non-XML ways is to use JSON, a cross platform Javascript human readable data format. I personally am using the JSON-simple library, just because it was the easiest to set up in Eclipse.

I also found there weren’t many examples of how to encode into, and decode out of, JSON using Java – so here is an example. (note that you need json-simple for this example, which you can download here.)

//Runnable ‘main’ java class

package JSONExamplePackage;

import org.json.simple.parser.ParseException;

/**
*
*
* @author Ben Delaporte
*
*/
public class JSONExample {

public static void main(String[] args) {
// TODO Auto-generated method stub

TestObject firstObject = new TestObject();

firstObject.Name = “Jim”;
firstObject.ID = 5;
firstObject.Money = 78910.3;

System.out.println (“Object before being put into JSON format:(” + firstObject.Name + “,” + firstObject.ID + “,” + firstObject.Money + “)”);
org.json.simple.JSONObject firstJSON = ReturnJSONfromTestObject(firstObject);
System.out.println(“Object in JSON format, before converting to string and parsing: ” + firstJSON.toString());

//Send firstJSON to a string
String testString = firstJSON.toString();

//Now make a new JSONObject, to parse the string into
org.json.simple.JSONObject secondJSON2 = new org.json.simple.JSONObject();

try {
//Use an org.json.simple,parser.JSONParser to parse the string
org.json.simple.parser.JSONParser testParser = new org.json.simple.parser.JSONParser();
secondJSON2 = (org.json.simple.JSONObject) testParser.parse(testString);
System.out.println(“Object in JSON format, after parsing from string: ” + secondJSON2.toString());
} catch (ParseException e) {

System.out.println(“Unable to parse JSON string ” + secondJSON2.toString());
}

TestObject secondObject = ReturnTestObjectfromJSON(secondJSON2);
System.out.println (“Object after conversion from JSON to TestObject type:(” + secondObject.Name + “,” + secondObject.ID + “,” + secondObject.Money + “)”);

}

public static org.json.simple.JSONObject ReturnJSONfromTestObject(TestObject customObject)
{

org.json.simple.JSONObject jsonObject = new org.json.simple.JSONObject();
jsonObject.put(“Name”, customObject.Name);
jsonObject.put(“ID”, customObject.ID);
jsonObject.put(“Money”, customObject.Money);
return jsonObject;
}

public static TestObject ReturnTestObjectfromJSON(org.json.simple.JSONObject jsonObject)
{

TestObject customObject = new TestObject();

customObject.Name = (String) jsonObject.get(“Name”);
customObject.ID = (Long) jsonObject.get(“ID”);
customObject.Money = (Double) jsonObject.get(“Money”);
return customObject;

}

}

//TestObject class

package JSONExamplePackage;
/**
*
*
* @author Ben Delaporte
*
*/
public class TestObject {

public String Name;

//Note* on choice of (long) type.
public long ID;
public double Money;
}

//Console output

Object before being put into JSON format:(Jim,5,78910.3)
Object in JSON format, before converting to string and parsing: {“Name”:”Jim”,”Money”:78910.3,”ID”:5}
Object in JSON format, after parsing from string: {“Name”:”Jim”,”Money”:78910.3,”ID”:5}
Object after conversion from JSON to TestObject type:(Jim,5,78910.3)

//*At first, I had TestObject.ID set up as an ‘int’ type. But the parser kept returning a ‘long’ type, which was throwing a ‘long can’t convert to int’ error. So I went ahead and changed TestObject.ID to ‘long’ type. Just a heads up.

Tags: Android, android socket, code example, Eclipse, Eclipse error, Java, java socket, JSON, JSON decoding, JSON encoding, JSON networking, JSON parser, JSON string, JSON-simple, mobile networking, networking, objectreader, objectwriter, serializable, socket, sockets

no comment

20

Mar

Fixing the Netbeans 6.9 Phantom Error/Bug/Red flag in a complex Java project

Posted by Ben  Published in Software Development

Hi everyone,

I’ve been doing a lot of Java development lately, designing a 2D multiplayer game involving 2D physics, a client package, a server package, a shared package, and serialized objects over the network. It’s a lot of fun; I’ve been working with my brother and gaining a lot of experience with network programming along the way.

Which brings me to the subject of this post: A problem I recently had with Netbeans was a ‘phantom error’, or ‘phantom bug’ that Netbeans would display as a little red ‘flag’ or ‘sign’ on my Java project. It would appear near one of my package folders, the source folder a level above it, and finally near the ‘project’ icon itself. But when I opened the package folder in the Netbeans file browser, or project browser, there were no files with the same ‘red flag’. And my project would ‘clean and build’ with no errors. And I think that this corruption may have been causing other side effects when I was debugging my code.

I combed Google looking for a solution last time (including trying things myself, like deleting folders in the project, moving around .java files, and restarting Netbeans) but I was unable to find one, ended up deleting the project, creating a new project, and copying/refactoring in the source code for each of my .java files. This was pretty time consuming, and I ended having to switch my project over to a new subversion repository.

This happened to me again today. I’m now using Mercurial to version control my source code, so I really didn’t want to delete my entire Netbeans project/Mercurial repository just to get rid of a ‘phantom bug’. After quite a few Google searches, I learned that it is Netbeans’ cache that is corrupted. Deleting this cache directory at “~/.netbeans/6.9/var/cache/index/” fixed the problem on a reload of Netbeans. (*warning* You may want to back up the folder first, in case there is something important there for your project.)

So ‘walah’! No more phantom bugs. Much easier than refactoring into a new Netbeans project. =D

Tags: bug, bugs, cache, corruption, debug project, debugging Netbeans, errors, Java, Java bug, Netbeans 6.9, Netbeans bug, Netbeans cache, Netbeans error, Netbeans phantom error, Netbeans project, Netbeans projects, Netbeans red flag, phantom bug, red flag, red flags

no comment

Blogroll

  • Ed Delaporte IV on IT Leadership
  • Ed The Dev

Meta

  • Register
  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Recent Entries

  • What is Maven? A Beginner’s Tutorial by Example
  • JSON code example in Java
  • OnyX for Mac – More Options for your Mac
  • Fixing the Netbeans 6.9 Phantom Error/Bug/Red flag in a complex Java project
  • Keeping Your Computer Safe and Speedy
  • The Animated Adventures of Boy ‘n Panda!
  • The Trophy
  • School is Out!
  • Second Place in Rube Goldberg Nationals
  • Curbing Your Startup Folder

Recent Comments

  • Starstreak in The Trophy
  • Ben in Second Place in Rube Goldberg Nationals
  • TheRandomOne in Second Place in Rube Goldberg Nationals
  • Root-dir in Second Place in Rube Goldberg Nationals
  • Ed The Dev .com » Blog Archiv… in Curbing Your Startup Folder
  • Random Selection of Posts

    • What is Maven? A Beginner’s Tutorial by Example
    • Second Place in Rube Goldberg Nationals
    • JSON code example in Java
    • School is Out!
    • New blog
    • The Animated Adventures of Boy ‘n Panda!
    • Curbing Your Startup Folder
© 2008 Ben Writes! is proudly powered by WordPress
Theme designed by Roam2Rome