Results 1 to 5 of 5
  1. #1
    avu
    avu is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default Error Accessing Methods in My JAR file

    Hello,

    PLease help me, I'm trying to add MY jar file to my java application and I'm trying to access the class this way.

    1) I had an import of the class I want to access.
    2) I create an instance of that that class and try to access the method inside the class, but Eclipse keeps giving the following errors as if it doesn't see the methods in the class...

    Syntax error on tokens, delete these tokens
    Syntax error on token(s), misplaced construct(s)


    ========CODE========
    This is just some sample code i had to see if it works to access my jar file...

    helloWorld hw = new helloWorld(); <--this is a class inside my jar no complains here
    hw.subtract(3,4); <---complains here
    hw.getStr(); <---complains here
    hw.subtract2(6,7); <---complains here


    This is the compiled class in my jar file.


    public class helloWorld{
    public helloWorld(){

    }
    public static int subtract(int x, int y){
    return x-y;
    }

    public int subtract2(int x, int y){
    return x-y;
    }

    public String getStr(){
    return "hello world";
    }


    }

    but it can see/recognize the class in the jar ok, just the methods in the class is not recognized! Eclipse can't seem to see the methods in the class i'm trying to access...

    Let me know if you need further clarifications...

    please help, I have a deadline soon.

    thanks in advance for your time,

  2. #2
    fxRichard is offline Member
    Join Date
    Dec 2008
    Posts
    64
    Rep Power
    0

    Default

    Did you wrap your calls in a main method? You will get this error if you didn't, meaning do this:

    Java Code:
    public static void main(String[] args)
    {
        helloWorld hw = new helloWorld();  //class names should be capitalized...fyi
        hw.subtract(3,4); //you do realize you are calling a static method from an instance...this will work but bad practice
        hw.getStr();
        hw.subtract2(6,7);
    }

  3. #3
    avu
    avu is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks for your reply fxRichard.

    public static void main(String[] args)
    {
    helloWorld hw = new helloWorld(); //class names should be capitalized...fyi
    avu: Yeah, I realized that after but I was just testing quickly so didn't pay attention to convention. :o

    hw.subtract(3,4); //you do realize you are calling a static method from an instance...this will work but bad practice
    avu: sorry why is this bad practise? Excuse my ignorance here.

    hw.getStr();
    hw.subtract2(6,7);
    }

    I didn't put it in a main method because we didn't need a main method.

    Is there any other way to get around it without a main method?
    The code in the jar was packaged so that we can reused that jar without having to access the source code.


    Thanks
    Last edited by avu; 03-22-2010 at 02:12 PM. Reason: grammar & wordings

  4. #4
    fxRichard is offline Member
    Join Date
    Dec 2008
    Posts
    64
    Rep Power
    0

    Default

    Can you provide the complete code for your test class/method that is using the code from the jar file, even the package and include statements, just copy paste it in here so we can see exactly what you are doing.

  5. #5
    avu
    avu is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default

    Hi fxRichard,

    I figured it out. You were on the right track when you said I had to put my code into the a main method. Well, I didn't need a main method but I put those code into a method and it works. Before i just had....

    public class c1 {
    //I need to put this code in a method DUh!! :(

    helloWorld hw = new helloWorld();
    hw.subtract(3,4);
    hw.getStr();
    hw.subtract2(6,7);
    }

    Anyway, thanks for your help much appreciated. :)

Similar Threads

  1. accessing instance variables from static methods
    By ravian in forum New To Java
    Replies: 7
    Last Post: 03-01-2009, 10:09 PM
  2. Accessing file
    By Doctor Cactus in forum New To Java
    Replies: 1
    Last Post: 10-24-2008, 07:31 PM
  3. Accessing Data from a .txt file
    By Oasis13 in forum New To Java
    Replies: 5
    Last Post: 02-01-2008, 12:16 AM
  4. Accessing a file randomly
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 08:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •