# Debugging help with Java for #OW9

**URL:** <https://forum.codecrafters.io/t/debugging-help-with-java-for-ow9/2524>\
**Category:** Challenges\
**Tags:** challenge:bittorrent\
**Created:** [October 22, 2024, 5:28pm UTC](https://forum.codecrafters.io/t/debugging-help-with-java-for-ow9/2524 "2024-10-22T17:28:59Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![JanInquisitor](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/janinquisitor/32/2514_2.png) [@JanInquisitor](https://forum.codecrafters.io/u/JanInquisitor)\
**Post date:** [October 22, 2024, 5:28pm UTC](https://forum.codecrafters.io/t/debugging-help-with-java-for-ow9/2524/1 "2024-10-22T17:28:59Z")

</div>

I’m stuck on Stage #ow9.

The test case is weird, it tries to use a file that it isn’t there but expects my solution to magically know the metainfo file announce key vaue

Here I’ll show you the logs:

```auto
remote: [tester::#OW9] Running tests for Stage #OW9 (Parse torrent file)
remote: [tester::#OW9] Running ./your_bittorrent.sh info /tmp/torrents4252367964/codercat.gif.torrent
remote: [tester::#OW9] Checking for tracker URL (Tracker URL: http://bittorrent-test-tracker.codecrafters.io/announce)
remote: [tester::#OW9] Expected stdout to contain "Tracker URL: http://bittorrent-test-tracker.codecrafters.io/announce", got: ""
remote: [tester::#OW9] Test failed

```

And here’s a snippet of my code:

```java
public class Main {
    private static final Gson gson = new Gson();

    private static final Bencode bencode = new Bencode();

    public static void main(String[] args) throws Exception {

        String command = args[0];

        if ("decode".equals(command)) {
            String bencodedValue = args[1];
            Object decoded;
            try {
                decoded = decodeBencode(bencodedValue);
            } catch (RuntimeException e) {
                System.out.println(e.getMessage());
                return;
            }
            System.out.println(gson.toJson(decoded));
        } else if ("info".equals(args[0])) {
            readInfoFile(args[1]);
        } else {
            System.out.println("Unknown command: " + command);
        }
    }

    private static void readInfoFile(String pathString) {
        try {
            Path path = Paths.get(pathString);

            if (!Files.exists(path)) {
                return;
            }

            byte[] torrentBytesArray = Files.readAllBytes(path);

            final Map<String, Object> dict = bencode.decode(torrentBytesArray, Type.DICTIONARY);

            final Object url = dict.get("announce");
            if (url == null) {
                System.out.println("Error: 'announce' field not found in torrent file.");
            } else {
                System.out.printf("Tracker URL: %s\n", url);
            }

            final Map<String, Object> info = (Map<String, Object>) dict.get("info");

            if (info != null) {
                System.out.printf("Length: %s\n", info.get("length"));
            } else {
                System.out.println("Error: 'info' field not found in torrent file.");
            }

        } catch (Exception e) {
            e.printStackTrace(); // Print the exception for debugging
        }
    }

    // I could use a stack or dequeue for when I decide to implement my own bencode solution.
    static Object decodeBencode(String bencodedString) {
        if (Character.isDigit(bencodedString.charAt(0))) {
            return bencode.decode(bencodedString.getBytes(), Type.STRING);
        } else if (bencodedString.startsWith("i")) {
            return bencode.decode(bencodedString.getBytes(), Type.NUMBER);
        } else if (bencodedString.startsWith("l")) {
            return bencode.decode(bencodedString.getBytes(), Type.LIST);
        } else if (bencodedString.startsWith("d")) {
            return bencode.decode(bencodedString.getBytes(), Type.DICTIONARY);
        } else {
            throw new RuntimeException("Only strings are supported at the moment");
        }
    }
    
}

```

---

<div class="post-metadata">

**Author:** ![rgbygv](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rgbygv/32/2502_2.png) [@rgbygv](https://forum.codecrafters.io/u/rgbygv)\
**Post date:** [October 23, 2024, 6:28am UTC](https://forum.codecrafters.io/t/debugging-help-with-java-for-ow9/2524/2 "2024-10-23T06:28:25Z")

</div>

well, the test will run in their environment, so you didn’t need worry about file don’t exist in your environment.try output more info to see what’s not working well

---

<div class="post-metadata">

**Author:** ![JanInquisitor](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/janinquisitor/32/2514_2.png) [@JanInquisitor](https://forum.codecrafters.io/u/JanInquisitor)\
**Post date:** [October 23, 2024, 2:49pm UTC](https://forum.codecrafters.io/t/debugging-help-with-java-for-ow9/2524/3 "2024-10-23T14:49:49Z")

</div>

Apparently it isn’t there because I get a “java.nio.file.NoSuchFileException”.

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [October 23, 2024, 2:54pm UTC](https://forum.codecrafters.io/t/debugging-help-with-java-for-ow9/2524/4 "2024-10-23T14:54:31Z")

</div>

@JanInquisitor looks like you’ve got a `!Files.exists(path)`, shouldn’t that have triggered an early return if the file doesn’t exist? Why are you getting an exception instead?

I’d also echo @rgbygv’s suggestion - try printing out the path value to make sure that args are being parsed correctly.

(Pretty sure the file exists btw, 1000s of users have passed this stage, and we haven’t changed the tester recently)

---

<div class="post-metadata">

**Author:** ![JanInquisitor](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/janinquisitor/32/2514_2.png) [@JanInquisitor](https://forum.codecrafters.io/u/JanInquisitor)\
**Post date:** [October 23, 2024, 2:58pm UTC](https://forum.codecrafters.io/t/debugging-help-with-java-for-ow9/2524/5 "2024-10-23T14:58:31Z")

</div>

I changed the code and made it print the exception, I get a “java.nio.file.NoSuchFileException”. I’ve also printed the arguments goings into the program and I got the path file string, here it is:

```shell
remote: [tester::#OW9] Running tests for Stage #OW9 (Parse torrent file)
remote: [tester::#OW9] Running ./your_bittorrent.sh info /tmp/torrents770425335/itsworking.gif.torrent
remote: [your_program] /tmp/torrents770425335/itsworking.gif.torrent.
remote: [your_program] java.nio.file.NoSuchFileException: /tmp/torrents770425335/itsworking.gif.torrent.
remote: [your_program] at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
remote: [your_program] at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106)
remote: [your_program] at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
remote: [your_program] at java.base/sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:261)
remote: [your_program] at java.base/java.nio.file.Files.newByteChannel(Files.java:379)
remote: [your_program] at java.base/java.nio.file.Files.newByteChannel(Files.java:431)
remote: [your_program] at java.base/java.nio.file.Files.readAllBytes(Files.java:3268)
remote: [your_program] at Main.readInfoFile(Main.java:40)
remote: [your_program] at Main.main(Main.java:28)
remote: [tester::#OW9] Checking for tracker URL (Tracker URL: http://bittorrent-test-tracker.codecrafters.io/announce)
remote: [tester::#OW9] Expected stdout to contain "Tracker URL: http://bittorrent-test-tracker.codecrafters.io/announce", got: "/tmp/torrents770425335/itsworking.gif.torrent.\n"
remote: [tester::#OW9] Test failed

```

```auto
remote: [your_program] [info, /tmp/torrents3409021111/congratulations.gif.torrent.]
remote: [your_program] File path doesn't exist.
remote: [tester::#OW9] Checking for tracker URL (Tracker URL: http://bittorrent-test-tracker.codecrafters.io/announce)
remote: [tester::#OW9] Expected stdout to contain "Tracker URL: http://bittorrent-test-tracker.codecrafters.io/announce", got: "[info, /tmp/torrents3409021111/congratulations.gif.torrent.]\nFile path doesn't exist.\n"
remote: [tester::#OW9] Test failed

```

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [October 23, 2024, 3:02pm UTC](https://forum.codecrafters.io/t/debugging-help-with-java-for-ow9/2524/6 "2024-10-23T15:02:07Z")

</div>

```auto
/tmp/torrents770425335/itsworking.gif.torrent.

```

@JanInquisitor is the “.” at the end of the filename part of the filename? Or was that added in your log? Seems a bit suspicious that the same dot would also be present in the “NoSuchFileException” log:

```auto
java.nio.file.NoSuchFileException: /tmp/torrents770425335/itsworking.gif.torrent.

```

If that’s not the issue here, feel free to [share your updated code](https://docs.codecrafters.io/features/publish-to-github#publish-to-github) and we’ll help figure this out!

---

<div class="post-metadata">

**Author:** ![JanInquisitor](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/janinquisitor/32/2514_2.png) [@JanInquisitor](https://forum.codecrafters.io/u/JanInquisitor)\
**Post date:** [October 23, 2024, 3:10pm UTC](https://forum.codecrafters.io/t/debugging-help-with-java-for-ow9/2524/7 "2024-10-23T15:10:47Z")

</div>

I have no idea where that “.” is coming from.

Here is my code:

```java
public class Main {
    private static final Gson gson = new Gson();

    private static final Bencode bencode = new Bencode();

    public static void main(String[] args) throws Exception {
        String command = args[0];

        if ("decode".equals(command)) {
            String bencodedValue = args[1];
            Object decoded;
            try {
                decoded = decodeBencode(bencodedValue);
            } catch (RuntimeException e) {
                System.out.println(e.getMessage());
                return;
            }
            System.out.println(gson.toJson(decoded));
        } else if ("info".equals(args[0])) {
            readInfoFile(args[1]);
        } else {
            System.out.println("Unknown command: " + command);
        }
    }

    private static void readInfoFile(String pathString) {
        try {
            Path path = Paths.get(pathString);

            byte[] torrentBytesArray = Files.readAllBytes(path);

            Map<String, Object> dict = bencode.decode(torrentBytesArray, Type.DICTIONARY);

            Object url = dict.get("announce");
            System.out.printf("Tracker URL: %s\n", url);

            Map<String, Object> info = (Map<String, Object>) dict.get("info");
            System.out.printf("Length: %s\n", info.get("length"));

        } catch (Exception e) {
            System.out.println(e);
        }
    }

    // I could use a stack or dequeue for when I decide to implement my own bencode solution.
    static Object decodeBencode(String bencodedString) {

        if (Character.isDigit(bencodedString.charAt(0))) {
            return bencode.decode(bencodedString.getBytes(), Type.STRING);
        } else if (bencodedString.startsWith("i")) {
            return bencode.decode(bencodedString.getBytes(), Type.NUMBER);
        } else if (bencodedString.startsWith("l")) {
            return bencode.decode(bencodedString.getBytes(), Type.LIST);
        } else if (bencodedString.startsWith("d")) {
            return bencode.decode(bencodedString.getBytes(), Type.DICTIONARY);
        } else {
            throw new RuntimeException("Only strings are supported at the moment");
        }

    }
}

```

---

<div class="post-metadata">

**Author:** ![rohitpaulk](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/rohitpaulk/32/6_2.png) [@rohitpaulk](https://forum.codecrafters.io/u/rohitpaulk)\
**Post date:** [October 23, 2024, 3:25pm UTC](https://forum.codecrafters.io/t/debugging-help-with-java-for-ow9/2524/8 "2024-10-23T15:25:32Z")

</div>

Gotcha. So the problem definitely seems to be related to the extra dot. @andy1li should be able to take a closer look at this for you on Monday. @JanInquisitor If you figure out what’s wrong in the meantime, please do let us know!

---

<div class="post-metadata">

**Author:** ![JanInquisitor](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/janinquisitor/32/2514_2.png) [@JanInquisitor](https://forum.codecrafters.io/u/JanInquisitor)\
**Post date:** [October 23, 2024, 4:07pm UTC](https://forum.codecrafters.io/t/debugging-help-with-java-for-ow9/2524/10 "2024-10-23T16:07:44Z")

</div>

The only way I could pass the test was modifying the method like this:

```java
    private static void readInfoFile(String pathString) {
        if (pathString.endsWith(".")) {
            StringBuilder sb = new StringBuilder(pathString);
            pathString = sb.substring(0, sb.length() - 1);
        }

        Path path = Paths.get(pathString);

        try {
            byte[] torrentBytesArray = Files.readAllBytes(path);

            Map<String, Object> dict = bencode.decode(torrentBytesArray, Type.DICTIONARY);

            Object url = dict.get("announce");
            System.out.printf("Tracker URL: %s\n", url);

            Map<String, Object> info = (Map<String, Object>) dict.get("info");
            System.out.printf("Length: %s\n", info.get("length"));

        } catch (Exception e) {
            System.out.println(e);
        }
    }

```

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [October 30, 2024, 2:12pm UTC](https://forum.codecrafters.io/t/debugging-help-with-java-for-ow9/2524/11 "2024-10-30T14:12:39Z")

</div>

@JanInquisitor Turns out there’s an extra dot at the end of the command in `your_bittorrent.sh`:

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/f/ff7b6eedcd46d4822faa71a1796003f5d03b86eb.png)

Removing it should resolve the issue with the trailing dot in the arguments.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [November 6, 2024, 8:56pm UTC](https://forum.codecrafters.io/t/debugging-help-with-java-for-ow9/2524/13 "2024-11-06T20:56:00Z")

</div>

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [November 11, 2024, 8:56pm UTC](https://forum.codecrafters.io/t/debugging-help-with-java-for-ow9/2524/14 "2024-11-11T20:56:05Z")

</div>

Closed on November 11, 2024, 8:56pm UTC

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
