1

I am trying to get my head wrapped around java classpath and .jar files and am trying to put the logic in one class and reference that class from another.
My current directory structure is:

C:\TEMP\StringOps
C:\TEMP\StringOps\StringOperations.java
C:\TEMP\StringOps\StringOperationsDriver.java

I have two classes:

package StringOps;

import java.lang.StringBuilder;

// To compile:  javac StringOps/StringOperations.java
public class StringOperations {

    private String data;

    public StringOperations(String input) {
            data = input;
    }

    public String ReverseString() {
        return new StringBuilder(data).reverse().toString();
    }

}

and my second class looks like:

package StringOps;

class StringOperationsDriver {
    public static void main(String[] args) {
        StringOperations sop = new StringOperations("hello world");

        System.out.println(sop.ReverseString());
    }
}

I can compile the code correctly by:

C:\TEMP>javac StringOps\StringOperationsDriver.java

But when I try to run the code:

C:\TEMP\java -cp . StringOps.StringOperationsDriver

I'm trying to get my head wrapped around this, then to move on to manifest files after I understand this. I've tried looking around at:

Java classpath and package problems - referencing multiple jar files

which is similar to the problem I'm having, but the following command from that url isn't working for me. I also have a .jar file in the same directory C:\TEMP.

Ideally, what I'm looking for is a way to put code in StringOperations.java, compile the code and put it into a jar file. Then access the contents of the .jar file from StringOperationsDriver.java. But since the last url didn't work for me, I figured I would step back and just try this to see if it works.

Community
  • 1
  • 1
coson
  • 8,301
  • 15
  • 59
  • 84
  • 1
    You have a typo: `Opertions`. – Kenney Dec 17 '15 at 19:45
  • First things first: make sure your driver class references the jar file: I mean a full reference not just a simple import because that isn't always enough and won't work if the jar file isn't referenced by the project. http://stackoverflow.com/a/19941941/2498017 – Richard Barker Dec 17 '15 at 19:48
  • @Kenney OP says he is able to compile the code with javac. When I run javac on a file that doesn't exist, I get an error saying that it can't find the file. That means the typo is probably a posting error, and not actually in his code. Clarification from the OP would be useful here. – Rainbolt Dec 17 '15 at 19:52
  • @Rainbolt - typo corrected – coson Dec 17 '15 at 19:56
  • Ah ok. You're missing `package StringOps` in the main class. Also, you don't have to import classes from the same package - but you had to, because of the missing `package` declaration. – Kenney Dec 17 '15 at 20:00
  • @Kenney, I corrected that, but when I still try to run 'java cp . StringOps.StringOperationsDriver', I still get the 'Error: Cold not find or load class StringOps.StringOperationsDriver' – coson Dec 17 '15 at 20:08
  • Works fine here. Have you recompiled? – Kenney Dec 17 '15 at 20:11
  • @Kenney, My apologies to you. That typo you noticed that I corrected did not make its changes to my source. Once I did that, everything worked the way it did. Again my humble apologies for taking your time. I wish there's a way to give you the answer. If you're interested, you could respond, then I'll give you credit. – coson Dec 17 '15 at 20:18
  • No problem, and no need to apologize - it's my choice to spend time here. I'm glad you got it sorted, and you saw that the problem wasn't so complicated after all, so I hope you feel more confident in your skills now! Happy coding! – Kenney Dec 17 '15 at 20:57

0 Answers0