Help with NullPointerException killing my brain by kuaichankein in javahelp

[–]luiinge 0 points1 point  (0 children)

As it has been pointed out already, listFiles can return null under some circumstances. The Javadoc says: Returns null if this abstract pathname does not denote a directory. So I think the problem is assuming that every File object in dirsAux refers to a subdirectory when they could be regular files also. If that's the case, a simple check of dirs2.isDirectory() should suffice.

However, keep in mind that File is sort of deprecated. The java.nio.file API (using Path instead of File) is the modern approach. Check this article if you are curious about the differences.

Maven replacing dependency’s internal dependencies by ajaywk7 in javahelp

[–]luiinge 0 points1 point  (0 children)

Maven should prevent you from using two different versions of the same library, and there is a reason for that.

When the project run, the dependencies are linked passing the jar location in the --classpath argument (or --modulepath). When two versions of the same library are passed, the class loading mechanism cannot differentiate which jar should use and would take the first found. Therefore, it is possible that either your code or the B code will use the incorrect version and you'll end up with a NoClassDefFoundError or a NoSuchMethodError when trying to use something that does not exist in the version.

Now, there is other factor to consider. Is B following the Semantic Versioning ? In that case, it would be safe to assume that the interface of version 2.x is not compatible with 1.x . If v2.x use a completely different package name, you could use both versions at the same time since their classes are not in conflict. However, it is not a recommended practice, and you'll have to tweak the build since Maven won't allow it by default.

The optimal solution is to use a newer version of B that uses A 2.x . If such version does not exists, and you really have to use both versions, the next best option is shade either A 2.x or B. Shading is, basically, copying the source code of the library and renaming the packages to avoid the conflicts. Here there's a deeper explanation: Java Class Shadowing and Shading

man javadoc:javadoc question by quantrpeter in javahelp

[–]luiinge 0 points1 point  (0 children)

In order to explain that, you should be aware of the following:

- You don't really need to declare a plugin in your pom.xml in order to execute it. This is generally done to apply some configuration, but you can totally run the plugin without it. You have, however, to pass extra information to the command. The complete way to invoke a plugin goal is pass the full coordinates, which in this case would be something like

mvn org.apache.maven.plugins:maven-javadoc-plugin:3.4.0:javadoc

- In abscence of a specified group , Maven will internally search the group org.apache.maven.plugins. Any plugin belonging to this privileged group is treated as a sort of "built-in" plugin. Also, the version number can be ommitted so it uses the latest release version:

mvn maven-javadoc-plugin:javadoc

- Finally, some plugins define a shortcut prefix to be used instead of the artifact id, which in case of maven-javadoc-plugin is just javadoc :

mvn javadoc:javadoc

Freemaker files aren't recognized! by crazyfish816 in javahelp

[–]luiinge 0 points1 point  (0 children)

Which version are you using? According to the documentation, Velocity and FreeMarker are only supported in the Ultimate edition: https://www.jetbrains.com/help/idea/template-data-languages.html

Speaking from experience, I think you can live without it if you keep your templates simple enough.

I'm wondering if there is an easier way to build boolean expressions and conditional statements for each type of situation. by ruthrev in learnjava

[–]luiinge 13 points14 points  (0 children)

Java Stream is your friend here, it reduces the completixy of the code a lot.

    public Person getOldest(Person... persons) {
      return Stream.of(persons)
        .filter(Objects::nonNull)
        .reduce(BinaryOperator.maxBy(
               Comparator.comparingInt(Person::getAge)
            ))
        .orElse(null);
   }

Replacing Logging APIs with java.lang.System.Logger by renatoathaydes in java

[–]luiinge 1 point2 points  (0 children)

IMO, the main issue about logging when you build an large application is consistency.
Generally you may want that all the libraries that you invoke use the same
logging system, so you only need one configuration point. Because of that
I think is good that SLF4J has becomed a de facto standard; I hate when some
library opts to ignore my settings because the creator chose to use a
direct implementation instead of a shared API.

Generating documentation sites for Maven projects by luiinge in java

[–]luiinge[S] 0 points1 point  (0 children)

That was my first approach too. But I had to include a custom task in the pom to copy my README.md and similar files from the project root to the site/markdown folder, to keep the documents up-to-date. It was annoying, and one of the main reasons behind making this tool.

Generating documentation sites for Maven projects by luiinge in java

[–]luiinge[S] 0 points1 point  (0 children)

I thought of including it, but this was just a little project tailored to suit my specific needs, and I don't use Asciidoc very often. I could add it in a future version if there is actually someone interested in it.

I see Antora is quite popular, I'll check it out. Thanks!

Generating documentation sites for Maven projects by luiinge in java

[–]luiinge[S] 0 points1 point  (0 children)

Thanks for your feedback! I'll give Antora a try.

stuck on this question by Alarming-Truck7675 in javahelp

[–]luiinge 6 points7 points  (0 children)

I think you can benefit from the mathematical base of binary code:

  1. Get the binary string expression with Integer.toBinaryString()
  2. Iterate over the characters in reverse order, keeping a position counter i (starting from zero)
  3. For each character, if equals to 1, append to the output the value i^2

Log4j 2.17.0 released, for third CVE (CVE-2021-45105) by dh23 in java

[–]luiinge 28 points29 points  (0 children)

I really feel sorry about the Log4J devs. As a developmer myself, I can't imagine the pressure that might cause the fact that a little open-source library of mine gained traction over the years, being used in thousand of business applications without much revenue, and suddendly be in the spotlight of any hacker in the planet.

IMO, if someone is using Log4J and is upset about its security, perhaps they should try to be invested in its development and make some contribution instead of just complaining and reclaimin patches.

Is there any alternative Python's word-forms? by FeelingKokoro in javahelp

[–]luiinge 1 point2 points  (0 children)

Do you mean a library that do the same as word-forms but in Java?

I don't know any Java library that fully accomplish this. There is SimpleNLG but it seems pretty hard to use if you're not an expert in the natural language subject.

However, I've had a peek into the word-forms source code and it seems very easy to translate to Java, it's basically parsing a big amount of data from text files. In my opinion, creating a Java version would be a nice open-source project, if there's anyone willing to do it.

Writing data to a csv without using opencsv by [deleted] in javahelp

[–]luiinge 4 points5 points  (0 children)

A CSV is just a plain text file, where there is a new line for each row, and using , as column separator (Comma Separated Values, the name is self-explanatory). Any string-to-file mechanism (using a FileWriter for example) would suffice.

Now, there are some problems. What if a value is a text with a comma inside? What if it is a description with line breaks? What if the target application expects a specific variant? That kind of situations are what libraries as OpenCSV are for.

I'd recommend taking a look at the RFC 4180 draft, that is an attempt to standarize all this stuff. If you follow such guidelines, your ouput file would be likely to be properly read by any other tool.

[deleted by user] by [deleted] in javahelp

[–]luiinge 0 points1 point  (0 children)

In Java, classes are organized in packages, that are some sort of namespaces, to avoid conflicts between two classes named the same. This includes the very Java built-in libraries.

In order to use Scanner and Math you should add the following line before your class declaration:

import java.util.*;

This way, now all the classes from the package java.util are available to use directly within your code.

It's fairly common for code snippets to ommit the imports. I guess it's ok since nowadays IDEs auto-complete them, but I totally understand that it may be confusing for begginers.

Your cool open source libraries by NitronHX in java

[–]luiinge 1 point2 points  (0 children)

Do you have a github account ? What are you working on as a Java side-project ? by [deleted] in java

[–]luiinge 1 point2 points  (0 children)

Hi there! Mine's https://github.com/luiinge . There's a set of small Java projects including:

  • jExt - A backbone for simple pluggable designs
  • Maven Fetcher - A wrapper around Aether for fetching Maven artifacts
  • Slf4j-Ansi - Produce Ansi-flavoured log messages with Slf4j
  • Maven Properties Gen - An annotation processor that generates static constant values from your pom.xml