How To: SPOJ TEST Using JAR File

September 12, 2010 at 07:39 (java, tutorial)

Background: Sphere Online Judge (SPOJ) is an online judge system accepting a wide variety of programming languages. It’s a great way to test your skills as a programmer, and can be quite addictive if you like a good challenge.

While browsing the SPOJ forums, I did not find a thread detailing how to successfully submit a JAR for the first problem, TEST. So, here’s a quick guide. I’m using Windows, but there should be little difference with other operating systems since everything is done through the command line. I will assume you already have the JDK installed and paths set up such that if you type java, javac, or jar from the command line the appropriate programs will be executed.

First, a simple Java implementation:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main (String[] args) throws Exception {
		BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
		String s;
		while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
	}
}

Note that since all input numbers are one or two digits, it’s all right to use startsWith(). Also, it seems the import statements cause a small drop in performance, but this is easier to read than specifying the package names in the main program.

Now, the simplest way to get AC with JAR is to save this code to a file Main.java. Then using the command line, go to the directory where your put your file and type

javac Main.java

as usual. Then in the same directory create a file manifest.txt (the name is arbitrary) containing this text:

Main-Class: Main

and make sure the file is terminated with a newline. Then from the command line type

jar cfm Main.jar manifest.txt Main.class

The .jar file is now complete, and if you’d like to execute it you can type

java -jar Main.jar

From Windows/DOS you can pipe input/output using files in the usual manner as follows

java -jar Main.jar < in.txt > out.txt

More information about JAR files can be found on Oracle’s Java Tutorials.

3 Comments

  1. kokosek said,

    Hi.
    First of all, I would like to thank You for this unique tutorial. :-)
    I rarely write programs in Java and if so I use ideone.com to execute them. Could You write me, what I have to do to execute Main.hava and make this JAR file?
    I downloaded JDK from here: http://www.oracle.com/technetwork/java/javase/downloads/jdk6-jsp-136632.html
    But there are lots of .exe files in “bin” folder and I don’t know where I can paste my Java code to execute it then.
    I would be really grateful if You helped me.

    • dansesacrale said,

      Hi! I recognize your name from SHORTEN — great job setting that up, and it’s an honour to have you here!

      I assume since you see .exe files that you are using some version of Windows. Here are some more details to allow you to follow the steps in the main post. Maybe I list things that are too basic, just to cover all bases.

      1) A little terminology: “directory” means folder, and “path” means location of a directory or file, such as “C:\mydir\myfile.txt”. (Absolute path gives full path, relative path is in relation to some starting location.)

      2) You can save plain text files using Notepad; for more features you can use a text editor with syntax highlighting like Notepad++, or an IDE like Eclipse. You can use internet search to get more info on any of these programs.

      3) It’s fine to save the file Main.java to anywhere on your computer.

      4) When saving Main.java, you may need to specify the file type as All Files (*.*) to prevent the extension .txt from being added to the end of your file.

      5) For the most flexibility, it’s best to add the path of your JDK bin directory to your PATH environment variable. It’s possible that it’s already there; the easiest way to check would be to open a command line console (step 6) and type something like “java -version” (step 8).

      To modify PATH in Vista you will need Administrator privileges, and can do:

      5a) Start > Right-click on Computer > Properties
      5b) On left side, Tasks > Advanced system settings
      5c) Click Advanced tab if not already selected
      5d) Near bottom, click button that says “Environment variables…”
      5e) In System variables (bottom half), find and select Path, then click Edit
      5f) The variable is a semicolon-separated list of directory paths. Append a semicolon and the path of your JDK bin directory. Follow the pattern of what’s already there. On my computer the path is “C:\Program Files\Java\jdk1.6.0_20\bin”.
      5g) Click OK, click OK again, etc.
      5h) If you have a command line console open, the changes will not take effect until you close it and open a new one.

      6) The command line console is a program called cmd. You can do an internet search to find how to open it.

      7) The main command you need to know is “cd” which allows you to change directory. You can use “cd \” to go to the root directory; you can use “cd ..” to go up one directory (towards root), and “cd mydir” to get to a directory named mydir, or “cd mydir\mysubdir” to get to that location; also, if you start to type a directory name and then press the tab key on your keyboard, you can cycle through any directories starting with the letters you typed. Incidentally this is basically the same as *nix systems, except *nix file systems use forward slash instead of backslash.

      Another useful command is “dir” which prints the directory contents, like “ls” does in *nix.

      8) To test that you set up the PATH variable right, you can type “java -version” from some directory other than the bin directory of your JDK installation. You should see your Java version info printed out. Also, just typing “java” will give you usage notes.

      9) So the entire process now is: open a console, use “cd” to get to the directory where you have put or intend to put Main.java and manifest.txt, and follow the instructions in the post.

      That turned out to be longer than I thought it would! I hope you found it useful, let me know if there are any issues.

      One final note: It’s not necessary to create JAR files in order to run Java programs on your computer with the command line. Just type “javac Main.java” to compile and “java Main” to run.

      • kokosek said,

        Thank You very much!! I can’t express how thankful I am. :-)
        I know the stuff connected with cmd but it other hints were very useful and I think someone will find these basic also helpful. :-)
        I succeeded in writeing program in JAR so one more time – thank You. It’s rarely what You did (this detailed explanation) because everyone think nowadays that all people are lazy and do nothing to solve their problem and use other people to solve it for them. Fortunately, there are still people like You. :-)

Leave a comment