This week's giveaway is in the Java/Jakarta EE forum.
We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line!
See welcome thread
Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!

Jesper de Jong

Java Cowboy
+ Follow
since Aug 16, 2005
Jesper likes ...
Android IntelliJ IDE Java Scala Spring
Forum Moderator
Jesper de Jong currently moderates these forums:
The Netherlands
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jesper de Jong

Jon Skeet, the top question answerer on StackOverflow, recently wrote a blog post about StackOverflow culture.
1 day ago
With the way you describe things, it is very hard to understand what you mean. Please use clear and simple language, instead of abstract and general terms such as an "engine".

What are you trying to do, and what problem are you experiencing exactly?

Are you trying to send a JAR file as an attachment via e-mail, and is your mail server blocking it? If yes, then that's most likely for security reasons. Some mail servers do not allow you to send any kind of file that might contain executable code, to protect the receiver of the e-mail from running arbitrary code received from an e-mail (which is one way that computer viruses are distributed).

If this is your problem, then it doesn't have anything to do with Java or JAR files, and is simply a security feature of the e-mail system. One simple way to fix it could be to change the extension of the JAR file to something else (".xyz" for example) and then asking the receiver to rename it back to ".jar" before running it.
1 month ago
Nowadays, if you let your money sit on a savings account, it's only losing its value because the interest rate on savings accounts is almost zero.

There are many different ways to invest money in stocks. A relatively safe and easy way is to buy from a fund, which is a collection of stocks that are managed by whatever company or bank manages the fund. (I'm not sure if I'm using the right terms here, I'm not an expert either). About two years ago I did this and since the stock market has done very well in the past year, it's now up about 12% since when I bought it. My intention with this money is to keep it for the long term (I'm a freelancer and have to take care of my own pension, I don't have an employer to puts money in a pension fund for me).

If you really want to trade stocks of different companies directly, then yes, you would have to do a lot of research and learn how to follow the market and understand how to make decisions for buying and selling.

I bought Bitcoin for a few hundred euros a few weeks ago. Not as a serious investment, but just to see what would happen. Bitcoin went up more than 10x last year so it would be nice if my few hundred € would be worth a few thousand € in a year, and it's not so much money that if I'd lose it, I'd be seriously distressed. (The past week the Bitcoin fell quite a bit because different countries have announced they want to regulate cryptocoins more strictly, so right now I'm at a € 150 loss).
2 months ago
403 means Forbidden. Maybe the website requires you to log in before you are allowed to view the webpage. It's also possible that the website detects that you are not a regular browser (maybe by looking at the User-Agent header) and block you, because it doesn't want automated bots to be able to read the page.

If you need to log in, you'll have to do more in your program than just try to read from the page. It's most likely easier to program that using a library such as Apache HttpClient - Java's standard classes for reading webpages are quite underpowered and don't (easily) allow you to do things like logging in on a website.
2 months ago
If you want all the benefits of reactive, async / non-blocking, you'll need to make the whole stack async / non-blocking. JDBC is indeed inherently a blocking API, so you can't build a fully reactive / non-blocking app if you need to access the database through JDBC.

Oracle seems to be working on a new, async / non-blocking database API, which is (provisionally) called ADBA, but there's not a lot of information on it available yet. It will probably take a few years before it's available. Besides the API itself, database vendors will also need to implement non-blocking drivers.
2 months ago
It's a bit hard to say without seeing the whole project, but it's probably not the findBy() call that causes the record to be created in the database, but something else in your project.

Spring Boot automatically configures and does a lot of things automatically, which is on one hand good because it makes things easy and you don't have to write a lot of code to make things work, but on the other hand it makes it difficult to understand exactly what happens when you run the application. One of the things Spring Boot can do is automatically pick up certain files to populate the database when you start the application. For example, if you're using a regular relational database, you can put a file named data.sql in src/main/resources and it will automatically be executed (see chapter 78 of the Spring Boot manual). Maybe a similar mechanism exists for MongoDB and you have a file somewhere that gets loaded into the database at startup.

If you want to know exactly what's happening at startup, you can start your Spring Boot application with the --debug option on the command line, which will cause Spring Boot to write a detailed auto-configuration report to the console, which is useful for finding out what exactly is happening.
2 months ago
Yes, I think it was a hit on the web a few years ago and I've also played it for a while. Did you succeed in getting to 2048?

If you're tired of playing it yourself, you can try writing a program that plays it for you. Some people have used AI to write a program to play 2048.
2 months ago
You correctly understood what "static" means.

The of() method of class LocalDate is a factory method. It's a static method because you don't call it on an existing LocalDate object. Instead, it creates and returns a new LocalDate object.
2 months ago

Pete Letkeman wrote:I did recently see an article stating that extension methods get compiled into static methods when transformed into bytecode.
As a result the more extension methods you have, the more static methods you have, and people have different views on static methods.

If you write Java code with lots of static methods, then you're probably doing something wrong - you're most likely writing non-object-oriented, procedural code. But that is not really relevant for Kotlin extension methods - the fact that the Kotlin compiler happens to implement extension methods under the covers using static methods on the JVM doesn't make extension methods somehow bad.

Extension methods are a way to add methods to existing classes after the fact - or, that's what they seem to be at first sight.

In Java, people often create "utils" classes. Maybe you've written a StringUtils class yourself or used methods from such a class. Several well-known libraries have their own sets of "utils" classes, for example Apache Commons Lang and Spring both have their own StringUtils classes with utility methods for strings.

With Kotlin, instead of creating utility methods in a StringUtils class, you can create extension methods for class String instead. You can call these extension methods on String objects, and calling such a method looks exactly the same as calling any other method from class String:

Extension methods are not really dynamically added to a class - they are instead like any other external method, so they can for example not access the private variables of the class they are designed for.
2 months ago
I've been using Kotlin at work, so far only for a few little tools that I use myself.  I haven't yet used it extensively with Spring 5 yet.

Spring 5 does officially support Kotlin; the Kotlin support in Spring 5 mainly consists of two things: (1) making sure many things are annotated with @Nullable / @NonNull annotations so that it plays nice with Kotlin's null-safe type system, and (2) a number of extension methods to make working with Spring more convenient.

I've found, and I've seen from other projects / people using Kotlin, that extension methods are one of the most useful and powerful features that people pick up once they're going beyond the basics with Kotlin.

I like Kotlin a lot, and I hope I get the chance to use it a lot more in future projects.
2 months ago
It would be a lot easier if you would add an explanation to your code, besides only the title "How can I fix this code?".

What is the code supposed to do? What is the problem - do you get a compiler error, or an error when you try to run it? Or does it run, but produce output that is different from what you expected? If that's the case, then what exactly did you expect, what does it do in reality and how does that differ from what you expected?

Just by looking at it I can already see an error in the very first line: you are trying to import a class named Sec from the package java.util, but that package does not contain a class named Sec. What was your intention with that line? You're not using the class Sec in the code, so it seems redundant.

There are more errors. For example, String Map<String, String> in line 9 and nre HashMap in line 12, and i+=2> in line 16. You'll need to fix those syntax errors first.
2 months ago
The past week there was a lot of fuss about two security problems in Intel and other CPUs. Like with other security bugs that have happened before, these two have gotten their own catchy names and even a logo: Meltdown and Spectre.

Are you worried about these bugs, do you think they will affect you?

Here's a good video explanation of how these bugs exactly work:

2 months ago
It is really not a good idea to prevent Windows from installing updates. Yes, it's sometimes annoying, but getting malware on your computer because you have an unpatched security hole is an order of magnitude more annoying.

See what well-known security researcher Troy Hunt has to say about it here: Don't tell people to turn off Windows Update, just don't
2 months ago
You can use Map with the stream API, but not in the same way as for example a List. Here is an example.

There are also special collectors, such as Collectors.toMap(...) and Collectors.groupingBy(...) that will collect the elements of a Stream into a Map.
Not when the type of the variable is Supplier<StringBuilder> because there would be no way to pass the argument to the constructor. A Supplier is a thing that takes no arguments and returns some object (in your example, a StringBuilder).

You could use Function<String, StringBuilder> instead:

A Function is a thing that takes one argument (in this case, a String) and that returns some object (in this case, a StringBuilder) - which matches the constructor of StringBuilder that you want to use.
3 months ago