Java Class Not Found Exception with JsonFactory

Discussion in 'Plugin Development' started by RussianHaxer, Dec 8, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    RussianHaxer

    So I am quite new to Java, and by that I mean like a week into Java, so a lot of it is still a mystery for me. Basically, I am making a SpigotMC plugin with Google API to fetch data from live streams. I am using Maven and when I run the programm there, it seems to work fine. However, when running it on an actual server, I get a weird error.

    Here is the whole error message:

    Code:
    org.bukkit.command.CommandException:Unhandled exception executing command 'chatcontrol'in plugin MinecraftButChatRuinsEverything v0.1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47)~[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149)~[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchCommand(CraftServer.java:710)~[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at net.minecraft.server.v1_14_R1.PlayerConnection.handleCommand(PlayerConnection.java:1641)~[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:1481)~[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:47)~[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at net.minecraft.server.v1_14_R1.PacketPlayInChat.a(PacketPlayInChat.java:1)~[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19)~[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18)[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(SourceFile:144)[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23)[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(SourceFile:118)[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:909)[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:902)[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127)[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:886)[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:819)[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at java.lang.Thread.run(Thread.java:834)[?:?]Causedby: java.lang.NoClassDefFoundError: com/google/api/client/json/JsonFactory
    at Commands.ChatControl.onCommand(ChatControl.java:33)~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45)~[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]...17 more
    Causedby: java.lang.ClassNotFoundException: com.google.api.client.json.JsonFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:471)~[?:?]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:135)~[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:81)~[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:588)~[?:?]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:521)~[?:?]
    at Commands.ChatControl.onCommand(ChatControl.java:33)~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45)~[spigot-1.14.4.jar:git-Spigot-56f8471-eafd742]
    I've seen people have the same problem, but I never saw a good or clear solution to that. I tried tinkering around on my own, but haven't had much success. I assume the problem is due to loading the classes somehow, but I have no idea what do with this information.

    Here is the class in which the problem occurs after its getChatId method is called:

    Code:
    packageCommands;
    
    import com.google.api.client.auth.oauth2.Credential;import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;import com.google.api.client.googleapis.json.GoogleJsonResponseException;import com.google.api.client.http.javanet.NetHttpTransport;import com.google.api.client.json.JsonFactory;import com.google.api.client.json.jackson2.JacksonFactory;
    
    import com.google.api.services.youtube.YouTube;import com.google.api.services.youtube.model.LiveBroadcastListResponse;
    
    import java.io.*;import java.security.GeneralSecurityException;import java.util.Arrays;import java.util.Collection;import java.util.Timer;import java.util.TimerTask;
    
    publicclassYouTubeLiveStreamAPI{staticString streamID;privatestaticfinalString CLIENT_SECRETS ="client_secret.json";privatestaticfinalCollection<String> SCOPES =Arrays.asList("https://www.googleapis.com/auth/youtube.readonly");
    
    privatestaticfinalString APPLICATION_NAME ="API code samples";privatestaticfinalJsonFactory JSON_FACTORY =JacksonFactory.getDefaultInstance();
    
    /**
    * Create an authorized Credential object.
    *
    * @return an authorized Credential object.
    * @throws IOException
    */publicstaticCredential authorize(finalNetHttpTransport httpTransport)throwsIOException{// Load client secrets.File test =newFile("src/main/resources/client_secret.json");System.out.println(test.exists());FileInputStreamin=newFileInputStream(test);// InputStream in = YouTubeLiveStreamAPI.class.getResourceAsStream(CLIENT_SECRETS);GoogleClientSecrets clientSecrets =GoogleClientSecrets.load(JSON_FACTORY,newInputStreamReader(in));// Build flow and trigger user authorization request.GoogleAuthorizationCodeFlow flow =newGoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, SCOPES).build();Credential credential =newAuthorizationCodeInstalledApp(flow,newLocalServerReceiver()).authorize("user");return credential;}
    
    /**
    * Build and return an authorized API client service.
    *
    * @return an authorized API client service
    * @throws GeneralSecurityException, IOException
    */publicstaticYouTube getService()throwsGeneralSecurityException,IOException,GoogleJsonResponseException{finalNetHttpTransport httpTransport =GoogleNetHttpTransport.newTrustedTransport();Credential credential = authorize(httpTransport);returnnewYouTube.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();}
    
    /**
    * Call function to create API service object. Define and
    * execute API request. Print API response.
    *
    * @throws GeneralSecurityException, IOException, GoogleJsonResponseException
    */publicstaticvoid getChatId(String videoId)throwsGeneralSecurityException,IOException,GoogleJsonResponseException{YouTube youtubeService = getService();// Define and execute the API requestYouTube.LiveBroadcasts.List request = youtubeService.liveBroadcasts().list("snippet");LiveBroadcastListResponse response = request.setId(videoId).execute();YouTubeNormalChatAPI.chatId = response.getItems().get(0).getSnippet().getLiveChatId();YouTubeNormalChatAPI.getService();Timer timer =newTimer();TimerTask task =newNormalChatTask();
    timer.schedule(task,0,5000);}}
    And here are my dependencies:


    Code:
    <dependency>
    <groupId>org.spigotmc</groupId>
    <artifactId>spigot-api</artifactId>
    <version>1.12.2-R0.1-SNAPSHOT</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>com.google.api-client</groupId>
    <artifactId>google-api-client</artifactId>
    <version>1.30.6</version>
    </dependency>
    <dependency>
    <groupId>com.google.oauth-client</groupId>
    <artifactId>google-oauth-client-jetty</artifactId>
    <version>1.30.4</version>
    </dependency>
    <dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-youtube</artifactId>
    <version>v3-rev212-1.25.0</version>
    </dependency>
    
    I know this is a long and complicated thing, but help would be really much appreciated!
     
  2. Offline

    DerDonut

    I do not work with maven that often but you have to write to the pom where the actual jar files are. Either next to your plugin or an external path
     
Thread Status:
Not open for further replies.

Share This Page