1

I am looking into IBM's quickfile API. Now I am a front-end developer and I do not know much about Java.

I would like to know how I can implement some of the sample code they provide on the developers website: here is one of the sample codes:

Here is an example of how to use the API to get a list of users. This Java program gets the list of users using a REST request, parses the result as a regular expression, and then prints out the total number of users to the console.

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.codec.binary.Base64;


public class QuickFileUserCount {
  public static void main(String[] args) {
  try {
    if (args.length < 4) {
      System.out.println("Usage is java QuickFileUserCount <host> <port> <userid> <password>");
      System.exit(0);
    }
    String quickFileServer   = args[0];
    String quickFilePort     = args[1];
    String quickFileUser     = args[2];
    String quickFilePassword = args[3];
    String quickFileUserQuery = "http://" + quickFileServer + ":" + quickFilePort + "/quickfile/rest/admin/users/0/0";
    URL url = new URL(quickFileUserQuery);
    HttpURLConnection uc = (HttpURLConnection) url.openConnection();
    uc.setRequestMethod("GET");
    uc.setDoInput(true);
    String uidpw = quickFileUser + ":" + quickFilePassword;
    String encodedPassword = Base64.encodeBase64String(uidpw.getBytes()); 
    uc.setRequestProperty("Authorization", "Basic " + encodedPassword);
    InputStream is = uc.getInputStream();
    StringBuffer sb = new StringBuffer();
    int c;
    while ((c = is.read()) != -1) {
      sb.append((char) c);
    }
    String ss = sb.toString();
    String ps = "(.*?)\"totalRows\":(.+?),.*";
    String userCount = ss.replaceAll(ps, "$2");
    System.out.println("\nNumber of QuickFile users on server <" + quickFileServer + "> = " + userCount);       
    uc.disconnect();
  } catch (Exception e) {
      System.out.println("Exception: " + e.getMessage());
    }
  }
}

Do I need to build a custom APP to see this in action? If so, how do I go about this? Since I am new at programming, where do I start?

I would appreciate any answer

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
David Van Staden
  • 1,739
  • 9
  • 34
  • 52
  • Probably would be easiest to run this from the command line you can look at http://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line for reference – chancea Jul 30 '13 at 19:37

2 Answers2

1

I understand that by "implement" you mean that you want to take this sample code and run it.

Each Java file contains a top-level class that's named the same as the file. In this case, the class's name is QuickFileUserCount and the file should be named QuickFileUserCount.java. If the file had a "package" statement at the top, then that would specify the directory that the file is in; since it doesn't, it'll be in whatever your working directory is.

Java programs start by calling the main method on some class. Any class can have a main method, and so you specify the class that you want to start running in.

One detail that makes this example a little more involved is the import from the Apache Commons. This tells Java that the QuickFileUserCount class uses the Base64 class from Apache Commons Codec. You'll need the Commons Codec library to compile or run this example.

It's usually easiest to use an IDE such as Eclipse to develop Java applications, but for a simple trial of this example:

  • Download the Commons Codec library from the Apache Web site. Unpack commons-codec-1.8.jar into a temporary directory.
  • Save the example with the filename QuickFileUserCount.java.
  • Compile it with the following command (you'll need to have the Java Development Kit installed):

    javac -classpath commons-codec-1.8.jar QuickFileUserCount.java
    

    The "classpath" option tells the Java compiler to look for the Base64 class inside that jar file.

  • Run it using the following command:

    java -classpath commons-codec-1.8.jar QuickFileUserCount
    

    Note that you don't use the ".java" this time. You should see the usage statement printed out.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • thank you very much. I will try this. Two questions: The last command you posted that I should run, where do i run in from and how will the output look like? Do I run it from within eclipse? Also, I am downloading eclipse now, is Eclipse Standard 4.33 sufficient together with the JDK? – David Van Staden Jul 31 '13 at 07:29
  • Regular Eclipse will not have any difficulties with this code, but you will still need to import the Commons Codec jar into your project. Use the steps in @Jace's answer. If you're running from Eclipse instead of from the command line, you can right-click on the class in the Navigator and Run As->Java Application. Eclipse will compile it, and the output will appear in the Console view. If you need more information, search for an introduction to writing console Java applications. – chrylis -cautiouslyoptimistic- Jul 31 '13 at 17:18
0

If your question is how to use the library (or binary) for use in your code, then all you need to do is import the library.

Depending on which IDE you use, this can be done various ways.

In Eclipse, for example, you can go to Project > Properties > Java Build Path > Libraries and click Add External JARs.... Navigate to your JAR file (the library), and then click OK. The library should now have its resources accessible through your code, and you can run example codes like what you have now.

There are many IDEs. The way you add a library may be different for each one. It may help to look at the website for your respective IDE to find out how to import Java libraries and binaries.

Jace J McPherson
  • 450
  • 3
  • 13