Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with Java Language Levels in IntelliJ IDEA

I've just started to program a Discord Bot with some simple functions in java with IntelliJ IDEA. Now I do have the problem that I have to use Language level 8 for a specific function but when I do that, it gives me an error and nothing which worked before will work now.

Code:

package main;
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.events.guild.voice.GuildVoiceJoinEvent;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
import net.dv8tion.jda.core.entities.Role;
import net.dv8tion.jda.core.entities.User;
import java.util.List;
import javax.security.auth.login.LoginException;

public class Main extends ListenerAdapter {

public static void main(String[] args) throws LoginException {
    JDABuilder builder = new JDABuilder(AccountType.BOT);

    String token = "Token of bot which i dont want to show here";

    builder.setToken(token);

    builder.addEventListener(new main.Main());

    builder.buildAsync();
}

@Override
public void onMessageReceived(MessageReceivedEvent event) {
    System.out.println("We received a message from " +
            event.getAuthor().getName()+": "+ event.getMessage().getContentDisplay());

    if(event.getMessage().getContentRaw().equals("!ping"))
    {
        event.getChannel().sendMessage("Pong!").queue();
    }
}

@Override
public void onGuildVoiceJoin(GuildVoiceJoinEvent event) {
    Role role = event.getGuild().getRoleById("575318189467107328");

    List<Member> list = event.getGuild().getMembersWithRoles(role);

    for (int i = 0; i < list.size(); i++) {
        User supporter = list.get(i).getUser();

        supporter.openPrivateChannel().queue((channel)->{channel.sendMessage("Jemand braucht Hilfe").queue();});
    }
}
}

This what I tried to do so far. My Problem appears in the line which says "supporter.openPrivate .....".

I expected that it'll work when I change the project and the module language level to 8 but it gave me this error:

Error:java: error: release version 5 not supported

When I don't change the language level it says this: Lambda Expressions are not supported at language level '5'

like image 810
JukingJoker Avatar asked May 07 '19 15:05

JukingJoker


1 Answers

The default Java version used by Maven is Java 5 (1.5) so I would reckon this is your issue.

Ensure you have the correct JDK for the language level you're trying to use Java 8 JDK. Set up your environment variables and follow the installation process as per the guidelines.

Afterwards, in your Maven or Gradle set up files (pom.xml or build.gradle), change the java release version like below.

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
</plugins>
</build>

Check your compiler settings: Settings/Preferences > Build, Execution, Deployment > Compiler > Java Compiler > Change Target Bytecode version to 1.8 for that particular module

like image 150
Dylan Avatar answered Nov 13 '22 10:11

Dylan