#java.lang.NoClassDefFoundError: com/fasterxml/jackson/dataformat/yaml/YAMLFactory

1 messages · Page 1 of 1 (latest)

halcyon scaffoldBOT
#

<@&987246399047479336> please have a look, thanks.

halcyon scaffoldBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

wary jackal
#

yes. and the full exception

wary jackal
#

why is the maven dependency yellow?

timber crescent
#

where is maven?

#

sorry, misunderstanding

manic verge
#

small sidequestion, are u using VC code ?

#

and line 45 of discordveri... is what ? ( no line numbers on any screenshot )

manic verge
#

with the small section u posted its quite hard to help. But since your using a file, i'd start looking there with debug mode

#

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.13.0</version>
</dependency>

#

version might be wrong, but do u have it ?

#

2.15.2 is latest, did u update your maven after adding that section ?

manic verge
#

what is this ?

#

so 4 strings from another source ?

ivory hill
#

make sure you right clicked + refreshed deps?

ivory hill
#

I mean, it seems like somehow the dependency just isn't there?

#

What are you using the yaml for?

#

Maybe we can take a stop and make Json work

#

Then get back to the yaml

#

well tbh this should work as is

#

hence why i want you to switch back to json for a moment, since it will rule out any issues with the yaml integration

wary jackal
#

it has a lot of odd choices in it, that's for sure

#

and some legacy things

#

File class, casts, using nodes instead of letting it deserialize to a pojo automatically,...

#

also catching exception

#

all of what i just said should be redone properly

#

not just that aspect

#

anyways. to fix ur problem at hand, it would be helpful to understand what the problem actually is

#

so please describe it in more detail

ivory hill
#

oh i see whats happening

#

hashmap.put returns a value

#

and thats what is being written

#
ObjectMapper objectMapper = new ObjectMapper();
HashMap<String, String[]> m = new HashMap<>();
m.put(k, v);
objectMapper.writeValue(new File(CLIENTS_YML_PATH), m);
#

try this

#

inside the try

#

.put returns the previous value associated with the key or null if there is no previous value

#

so when you do new HashMap<>().put(...) you get null

#

which is all the hint i need to say this

#

STOP FUCKING USING CHAT GPT TO WRITE CODE FUCK

#

AAAGHGHGH

#
objectMapper.writeValue(new File(CLIENTS_YML_PATH), Map.of(k, v));
#

this will work too

#

yeah and chat gpt doesn't know about it

#

since it learned java from all the shitty articles ever written and the horrible 90s codebases

#

also - if you don't need yaml consider using my json library

#

nah its good

#

i just made a library specifically tailored for beginners

#
try (var writer = Files.newBufferedWriter(Path.of(CLIENTS_YML_PATH))) {
   Json.write(
       writer, 
       Json.objectBuilder()
         .put(k, Arrays.stream(v).map(Json::of).toList())
         .build()
   )
}
#

i think objectMapper.readValue(???, String[].class)

#

or just

#

actually

#

objectMapper.readValue(file, new TypeToken<HashMap<String, String[]>>() {})

#

for jackson

wary jackal
#

or just create a pojo like any sane person would do

ivory hill
#

well in this case isn't the set of keys open?

#

im not sure the annotation that "flattens" things

wary jackal
#

in which case u should alter the json. cause json with undefined keys is horrible bullshit

ivory hill
#
@JsonCreator
record Stuff(HashMap<String, String[]> value) {}
#

i think

#

and then read in Stuff.class

wary jackal
#

its just a shitty data model

#

do proper json

#

with well defined keys and then working with it is simple

ivory hill
#

key value pairs are fine - idk what they are using them for tho

wary jackal
#

keys are not values

#

bad:
{
day0: 30,
day1: 27,
day2: 35
day3: 21
}

#

better:
{
measurements: [
{ day: 1, temp: 30},
{ day: 2, temp: 27} ,
...
]
}

#

and suddenly its super easy to work with

#

and u can serialize in a single simple line of code with a pojo

ivory hill
#

yeah - jackson really guides you to certain json structures

#

since some are easy to work with, some suck

#
public class Main {
    static String CLIENTS_YML_PATH = "test.json";

    public static void main(String[] args) throws IOException {
        Map<String, String[]> stuff;
        try (var reader = Files.newBufferedReader(Path.of(CLIENTS_YML_PATH))) {
            Json json = Json.read(reader);
            stuff = JsonDecoder.object(
                    json,
                    JsonDecoder.array(JsonDecoder::string)
                            .map(list -> list.toArray(String[]::new))
            );
        }

        System.out.println(stuff);
    }
}
#

anyways

#

this is how you would do that with my library

#

where everything is uniformly painful

pure surge
#

you shouldn't need any annotation

#

depending of what you want

marsh sun
#

the only annotation you really need is JsonProperty on some fields for mapping json name with variable

#

right?

ivory hill
#

Afaik the ways to get that are

#
  1. Hash map subclass
  2. Type token
  3. Some annotation (which I think is Json creator)
pure surge
#

i think

#

try and see i guess

ivory hill
#

im pretty sure without the annotation it will look for

#
{
  "value": {
    "a": [ "b", "c" ],
    "d": [ "e" ]
  }
}
#

when what you want is for it to read

#
{
    "a": [ "b", "c" ],
    "d": [ "e" ]
}
pure surge
ivory hill
#

it will not

#

just tested it

pure surge
#

ah I see

#

there is a weird behavior happening here

ivory hill
#

json creator wont work either for some reason

pure surge
#

yes that's normal

ivory hill
#

jackson really guides you to certain json structures
since some are easy to work with, some suck

but i don't think this claim is just me being bad at jackson

pure surge
#

it's because it's a record

#

when you have a record, jackson except that it basically match the json, because a record represents data

#

there are three things you can do here :

ivory hill
#
public record StuffFlat(Map<String, String[]> value) {
    @JsonCreator
    public static StuffFlat from(Map<String, String[]> value) {
        return new StuffFlat(value);
    }
}
#

that works, but this doesn't

#
public record StuffFlat(Map<String, String[]> value) {
    @JsonCreator
    public StuffFlat {
        
    }
}

(+ still doesn't with the args written/overrode)

#

so thats interesting and fun

pure surge
#

it's because it's a record
when you have a record, jackson except that it basically match the json, because a record represents data
there are three things you can do here :

  • Use a factory :
record Container(Map<String, String[]> values) {
    @JsonCreator
    public static Container foo(Map<String, String[]> values) {
        return new Container(values);
    }
}
  • Use a class
class Container {
    Map<String, String[]> values;
    @JsonCreator
    public Container(Map<String, String[]> values) {
        this.values = values;
    }
}
  • Don't use a container :
Map<String, String[]> values = objectMapper.readValue(json, new TypeReference<>(){});
ivory hill
#
  • Make a container
class Container extends HashMap<String, String[]> {}
pure surge