0

Hi there I'm doing a capture the flag event and the current problem is in Java but I don't have any experience working with Java. When I try to run the file I get "could not find or load main class Espresso". I've seen other posts on here and I tried java -cp . Espresso but didn't have any luck. I did run javac Espresso.java to compile it before running java Espresso. Any help would be appreciated here is the code:

package com.hackinglab.ctf;
import java.io.PrintStream;

public class Espresso { public Espresso() {} private static String key = "Stringab";
  private static String cipher = "'>xgx47#)~Fp?8k4%IHsC9_a";

  public static void main(String[] paramArrayOfString) {
    if (paramArrayOfString.length != 1) {
      System.out.println("You must enter the flag.");
      System.exit(-1);
    }
    String str = paramArrayOfString[0];
    System.out.println(str);
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < cipher.length(); i++) {
      localStringBuffer.append((char)(key.charAt(i % key.length()) - cipher.charAt(i) + 55));
    }
    if (localStringBuffer.toString().equals(str)) {
      System.out.println("Flag correct, well done!");
    } else {
      System.out.println("Dude, that's wrong!");
    }
  }
}
TRR88
  • 5
  • 1
  • 5

2 Answers2

1

The package is part of the class name, and when stored on the disk the package structure is embodied by the folder structure (even in a JAR file, it is still a folder structure). So one "correct" solution would be

mkdir bin
javac -d bin Espresso.java
java -cp bin com.hackinglab.ctf.Espresso

Note that your source folder should also mirror the package structure (so the compiler can find things when you have more than one class). So the more usual sequence of commands might be something like

mkdir bin
mkdir -p "src/com/hackinglab/ctf"
mv Espresso.java src/com/hackinglab/ctf # to place the Espresso.java in the correct folder
javac -d bin -cp src src/com/hackinglab/ctf/Espresso.java
java -cp bin com.hackinglab.ctf.Espresso

Giving a directory tree like

├── bin
│   └── com
│       └── hackinglab
│           └── ctf
│               └── Espresso.class
└── src
    └── com
        └── hackinglab
            └── ctf
                └── Espresso.java

8 directories, 2 files

Then you might move on to create a jar file. You'll need a manifest file to set the application's entry point so that might look like

cat <<EOF > src/manifest.txt
Manifest-Version: 1.0
Main-Class: com.hackinglab.ctf.Espresso
EOF
cd bin
jar cfm espresso.jar ../src/manifest.txt com/hackinglab/ctf/*.class

The tree looks something like

├── bin
│   ├── com
│   │   └── hackinglab
│   │       └── ctf
│   │           └── Espresso.class
│   └── espresso.jar
└── src
    ├── com
    │   └── hackinglab
    │       └── ctf
    │           └── Espresso.java
    └── manifest.txt

8 directories, 4 files

And in the bin folder you can run

java -jar espresso.jar

Or from any other folder with

java -jar /path/to/espresso.jar
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

Either make sure you have the same folder structure as your package name

../com/hackinglab/ctf/Espresso.java

then you can build and run it like

javac com/hackinglab/ctf/Espresso.java
java com/hackinglab/ctf/Espresso

or remove the line package com.hackinglab.ctf; from your source file

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52