- How to solve an IllegalArgumentException in Java?
- Reasons for java.lang.IllegalArgumentException
- Example1
- Output
- Steps to solve IllegalArgumentException
- Example2
- Output
- Saved searches
- Use saved searches to filter your results more quickly
- FATAL ERROR: java.lang.IllegalArgumentException: Invalid relative name: META-INF\services\javax.annotation.processing.Processor #340
- FATAL ERROR: java.lang.IllegalArgumentException: Invalid relative name: META-INF\services\javax.annotation.processing.Processor #340
- Comments
- How to Solve IllegalArgumentException in Java?
- Java
How to solve an IllegalArgumentException in Java?
An IllegalArgumentException is thrown in order to indicate that a method has been passed an illegal argument. This exception extends the RuntimeException class and thus, belongs to those exceptions that can be thrown during the operation of the Java Virtual Machine (JVM). It is an unchecked exception and thus, it does not need to be declared in a method’s or a constructor’s throws clause.
Reasons for java.lang.IllegalArgumentException
- When Arguments out of range. For example, the percentage should lie between 1 to 100. If the user entered 101 then an IllegalArugmentExcpetion will be thrown.
- When argument format is invalid. For example, if our method requires date format like YYYY/MM/DD but if the user is passing YYYY-MM-DD. Then our method can’t understand then IllegalArugmentExcpetion will be thrown.
- When a method needs non-empty string as a parameter but the null string is passed.
Example1
public class Student < int m; public void setMarks(int marks) < if(marks < 0 || marks >100) throw new IllegalArgumentException(Integer.toString(marks)); else m = marks; > public static void main(String[] args) < Student s1 = new Student(); s1.setMarks(45); System.out.println(s1.m); Student s2 = new Student(); s2.setMarks(101); System.out.println(s2.m); >>
Output
45 Exception in thread "main" java.lang.IllegalArgumentException: 101 at Student.setMarks(Student.java:5) at Student.main(Student.java:15)
Steps to solve IllegalArgumentException
- When an IllegalArgumentException is thrown, we must check the call stack in Java’s stack trace and locate the method that produced the wrong argument.
- The IllegalArgumentException is very useful and can be used to avoid situations where the application’s code would have to deal with unchecked input data.
- The main use of this IllegalArgumentException is for validating the inputs coming from other users.
- If we want to catch the IllegalArgumentException then we can use try-catch blocks. By doing like this we can handle some situations. Suppose in catch block if we put code to give another chance to the user to input again instead of stopping the execution especially in case of looping.
Example2
import java.util.Scanner; public class Student < public static void main(String[] args) < String cont = "y"; run(cont); >static void run(String cont) < Scanner scan = new Scanner(System.in); while( cont.equalsIgnoreCase("y")) < try < System.out.println("Enter an integer: "); int marks = scan.nextInt(); if (marks < 0 || marks >100) throw new IllegalArgumentException("value must be non-negative and below 100"); System.out.println( marks); > catch(IllegalArgumentException i) < System.out.println("out of range encouneterd. Want to continue"); cont = scan.next(); if(cont.equalsIgnoreCase("Y")) run(cont); >> > >
Output
Enter an integer: 1 1 Enter an integer: 100 100 Enter an integer: 150 out of range encouneterd. Want to continue y Enter an integer:
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FATAL ERROR: java.lang.IllegalArgumentException: Invalid relative name: META-INF\services\javax.annotation.processing.Processor #340
FATAL ERROR: java.lang.IllegalArgumentException: Invalid relative name: META-INF\services\javax.annotation.processing.Processor #340
Comments
I cloned https://github.com/googleads/google-ads-java.git to my windows 10 desktop.
When I do a mvn compile, I get this error message:
Which version of the client library are you using?
Google Ads API Client Library for Java — v9.0.0
(https://github.com/googleads/google-ads-java/releases/tag/9.0.0)
Which version of Java are you using?
JDK 1.8
Which operating system (Linux, Windows, . ) and version?
Windows 10
Actions taken
- I went to the folder where the git was cloned to C:\Users\dhegde1\projects\google-ads-java
- mvn clean compile
Expected result
Expect compilation to complete succesfully without errors
Actual result
ERROR] FATAL ERROR: java.lang.IllegalArgumentException: Invalid relative name: META-INF\services\javax.annotation.processing.Processor
at com.sun.tools.javac.file.JavacFileManager.getFileForOutput(JavacFileManager.java:737)
at com.sun.tools.javac.processing.JavacFiler.getResource(JavacFiler.java:467)
at com.google.auto.service.processor.AutoServiceProcessor.generateConfigFiles(AutoServiceProcessor.java:171)
at com.google.auto.service.processor.AutoServiceProcessor.processImpl(AutoServiceProcessor.java:116)
at com.google.auto.service.processor.AutoServiceProcessor.process(AutoServiceProcessor.java:104)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:802)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$200(JavacProcessingEnvironment.java:91)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors$ProcessorStateIterator.runContributingProcs(JavacProcessingEnvironment.java:635)
at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1041)
at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1206)
at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170)
at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:856)
at com.sun.tools.javac.main.Main.compile(Main.java:523)
at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)
at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)
at org.codehaus.plexus.compiler.javac.JavaxToolsCompiler.compileInProcess(JavaxToolsCompiler.java:126)
at org.codehaus.plexus.compiler.javac.JavacCompiler.performCompile(JavacCompiler.java:174)
at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:1129)
at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:188)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
Anything else we should know about your project / environment
I have attached the mvn log
The text was updated successfully, but these errors were encountered:
How to Solve IllegalArgumentException in Java?
An unexpected event occurring during the program execution is called an Exception. This can be caused due to several factors like invalid user input, network failure, memory limitations, trying to open a file that does not exist, etc.
If an exception occurs, an Exception object is generated, containing the Exception’s whereabouts, name, and type. This must be handled by the program. If not handled, it gets past to the default Exception handler, resulting in an abnormal termination of the program.
IllegalArgumentException
The IllegalArgumentException is a subclass of java.lang.RuntimeException. RuntimeException, as the name suggests, occurs when the program is running. Hence, it is not checked at compile-time.
IllegalArgumentException Cause
When a method is passed illegal or unsuitable arguments, an IllegalArgumentException is thrown.
The program below has a separate thread that takes a pause and then tries to print a sentence. This pause is achieved using the sleep method that accepts the pause time in milliseconds. Java clearly defines that this time must be non-negative. Let us see the result of passing in a negative value.
Program to Demonstrate IllegalArgumentException:
Java
Exception in thread "Test Thread" java.lang.IllegalArgumentException: timeout value is negative at java.base/java.lang.Thread.sleep(Native Method) at Main$1.run(Main.java:19) at java.base/java.lang.Thread.run(Thread.java:834)
In the above case, the Exception was not caught. Hence, the program terminated abruptly and the Stack Trace that was generated got printed.
Diagnostics & Solution
The Stack Trace is the ultimate resource for investigating the root cause of Exception issues. The above Stack Trace can be broken down as follows.
Part 1: This part names the Thread in which the Exception occurred. In our case, the Exception occurred in the “Test Thread”.
Part 2: This part names class of the Exception. An Exception object of the “java.lang.IllegalArgumentException” class is made in the above example.
Part 3: This part states the reason behind the occurrence of the Exception. In the above example, the Exception occurred because an illegal negative timeout value was used.
Part 4: This part lists all the method invocations leading up to the Exception occurrence, beginning with the method where the Exception first occurred. In the above example, the Exception first occurred at Thread.sleep() method.
From the above analysis, we reach the conclusion that an IllegalArgumentException occurred at the Thread.sleep() method because it was passed a negative timeout value. This information is sufficient for resolving the issue. Let us accordingly make changes in the above code and pass a positive timeout value.
Below is the implementation of the problem statement: