5

I am trying to compile my java file name Test.java. Test.java calling a class com.api.APIUser.java which is available in a user.jar file. I have added user.jar in lib folder. But Test.java is unable to pick APIUser.java. While I am compiling Test.java using javac I am getting error

"package com.api does not exist".

Test.java

import com.api.APIUser;
 public class Test{
  APIUser ap = new APIUser();
  ap .login();
  public static void main(String[] args){
    //to do
  }

}

APIUser

package com.api
public class APIUser{
  public string login(){
   //to do
   return string;
 }

}

If any one have idea why I am getting this error.please suggest me solution. Thanks in advance.

user3090158
  • 135
  • 1
  • 3
  • 14
  • Possible duplicate of [How to make javac find JAR files? (Eclipse can see them)](http://stackoverflow.com/questions/7182465/how-to-make-javac-find-jar-files-eclipse-can-see-them) – Prim Dec 10 '15 at 08:07
  • You need to pass the JAR in the CLASSPATH. If you are using Eclipse IDE you can try this, http://stackoverflow.com/questions/179024/adding-a-jar-to-an-eclipse-java-library – Subir Kumar Sao Dec 10 '15 at 08:07
  • thanks subir for reply,But i am not using eclipse ,using command i am doing all the work.so please tell us the solution how to do it using command. – user3090158 Dec 10 '15 at 08:09
  • use javac -cp .;/lib/user.jar -D com.api.Test.java – Vivek Singh Dec 10 '15 at 08:12
  • @user3090158 You really should start using an IDE like Eclipse or Netbeans. You have several syntax errors in your code, which are easily detectable using an IDE. – MC Emperor Dec 10 '15 at 08:31
  • If you are using command line, then you are using some text editor. First Configure it for java extension. few problems will be solved away, next when you compile/run your executable, add respective path to jar – bananas Feb 21 '20 at 07:08

2 Answers2

3

put a semicolon after the package com.api like as below

package com.api;

clean and build the project and run if any issue inform

avishka eranga
  • 105
  • 1
  • 11
1

You have multiple issues in your code.

  1. You have no line termination present for the com.api import in the APIUser class;
  2. You have a syntax error in your login method.

Below is the improved code:

import com.api.APIUser;

public class Test {
    // APIUser ap = new APIUser(); // This call should be in the method body,
    // there is no use to keep it at the class level
    // ap.login(); // This call should be in method body
    public static void main(String[] args) {
        // TO DO
        APIUser ap = new APIUser();
        ap.login();
    }
}

APIUser

package com.api; // added termination here

public class APIUser {
    //access specifier should be public
    public string login(){
       //to do
       //return string;//return some value from here, since string is not present this will lead to error
         return "string";
     }
}

Also be sure that the JAR file is present in the classpath. If you are not using any IDE, you must use the -cp switch along with the JAR file path, so that a class can be loaded from there.

You can use the code below to understand how to compile your class using classpath from command prompt.

javac -cp .;/lib/user.jar; -D com.api.Test.java
Community
  • 1
  • 1
Vivek Singh
  • 2,047
  • 11
  • 24