1

I hav created a jar file named CustomValidations.jar which includes a single class file named CustomValidation.java

 package validation; public class CustomValidation { public boolean matchFields(String str1,String str2){ if (!str1.equals(str2)) { return false; } else { return true; } } } 

I created another simple java project and I need to call the matchFields method

 package com.codes; import CustomValidation.*; public class TestClass { public boolean CheckEmail(){ if(matchFields("sowmya","sowmya")){ System.out.println("Matched"); } else { System.out.println("Not Matched"); } } } 

It throws me an error saying "The import CustomValidation cannot be resolved" What is the correct way to call a method?

4
  • You will have to add this project as dependency to the project in which you are using that code.. Commented Mar 19, 2015 at 6:01
  • matchFields isn't static, you'll need an instance of CustomValidation before you can use it... Commented Mar 19, 2015 at 6:03
  • I hav added this jar file in the BuildPath..Is there anything else needed Commented Mar 19, 2015 at 6:04
  • U said that ur jar has a class file with name "CustomValidation.java", I think u have added source file in your jar. Please add "CustomValidation.class" instead. Commented Mar 19, 2015 at 6:22

2 Answers 2

1

Your import was wrong

instead of

 import CustomValidation.*; 

it should be

import validation.*; 

and also method is not static so you need to create Instance to access that method. as:

new CustomValidation().matchFields("sowmya","sowmya"); 
Sign up to request clarification or add additional context in comments.

Comments

0

matchFields isn't static, you'll need an instance of CustomValidation before you can use it. The important statement is slightly wrong as well, normally it's import {package path}.{class name} or in your case import validation.CustomValidation (or even import validation.*)

import validation.CustomValidation; public class TestClass { public boolean CheckEmail(){ CustomValidation customValidation = new CustomValidation(); if(customValidation.matchFields("sowmya","sowmya")){ System.out.println("Matched"); } else { System.out.println("Not Matched"); } } } 

You could make the method static

public class CustomValidation { public static boolean matchFields(String str1,String str2){ if (!str1.equals(str2)) { return false; } else { return true; } } } 

And then do a static import

import static validation.CustomValidation.matchFields; public class TestClass { public boolean CheckEmail(){ if(matchFields("sowmya","sowmya")){ System.out.println("Matched"); } else { System.out.println("Not Matched"); } } } 

This all assumes that the jar file containing the CustomValidation is available within the classpath when the TestClass is built and run

3 Comments

I tried with the above changes bt still getting the error Multiple markers at this line - The import validation cannot be resolved - The import validation.CustomValidation cannot be resolved.......The method matchFields(String, String) is undefined for the type TestClass
The it sounds like the Jar either doesn't contain the class or you've not included the jar within the classpath for the project. Try extracting the contents of the Jar file and make sure it contains the class you expect in the directories represented by the class package declarations.
God...I missed to include the option "Export generated class files and resources" while creating the jar file...It is working perfectly..................Thank you @ MadProgrammer