tags:

views:

118

answers:

2

I am joining a competition that requires me to put all my java classes in one single .java file. Does there exist a tool that does this for me (including changing the visibility of classes to be able to do this)?

Addition: thanks for trying to help me to read the site of the competition but I quote:

It is possible to make more than one class for your program, but you will have to put the source for all classes in a single .java file (the compiler will produce multiple .class files anyway). When you do this, you should not declare your classes public, or the compiler will complain about it.

So, only 1 .java file is allowed (no jar) and in that file I can have multiple non-public classes besides my public main class (and not only static inner classes as suggested).

+3  A: 

If you have access to Unix-y shell (for Windows, you can install e.g. Git for a decent Bash implementation, and it gives you a great VC tool):

cat *.java | sed 's/public class/class/g' >AllTehCodez.java

Doesn't have to be more complicated than that (unless you have a lot of strings containing the substring "public class", of course).

Edit: Doesn't work for package and imports. But...

(
  egrep -h ^package *.java | head -1
  egrep -h ^import *.java | sort -u
  egrep -hv '^(import|package)' *.java | sed 's/public class/class/g' 
) >AllTehCodez.java

This does of course assume all the classes are in the same package.

gustafc
close - package and import declarations kill that solution. You'll have to edit the result after the merge.
Andreas_D
Oh, right. I'll fix that.
gustafc
A: 

If we exclude various "bijou scripting haquettes" along the lines suggested above, I seriously doubt that any serious tool exists for doing this.

Why? Because this kind of nonsense goes against all known Java style rules and conventions!

The people behind that website need to be taught about archive file formats; e.g. TAR, ZIP, JAR.

EDIT

I take that back. They DO understand JAR files. Quoting from one of their documents:

Using your own executable Java jar with Caia

You can also use your own Java jar in Caia competitions. We have written the jarwrapper for that. The source of jarwrapper.c is put in the caia_install_/jarwrapper/ folder. In the Windows distro this is put into the src/ folder.

Suppose the name of your class file is JavaPlayer.class. The only thing you will have to do is to rename the executable in the bin/ folder from jarwrapper to JavaPlayer. The executable now will perform the command: java -jar JavaPlayer. The jar file should contain a manifest which points to the class with the main method.

In manager.txt you can use the program name JavaPlayer which refers to the executable that starts your Java jar player.

Stephen C
Yes, their software supports it, but for the real competition there is clearly in the rules that it must be a single .java source file. Note that they want source files, and that the part you quoted now is about compiled files (java -jar)
Peter Smit