2

A basic question: I'm using the command line to compile and run my java code (javac and java commands respectively). Our professor provided us with a .class file we can use in our future projects. The class filename is U.class, and our professor told us which methods are in included in this class so we can invoke them in our own code. My question is, how can I tell Java I want to use this .class file in my code and how would I type it in the command line so to compile this (using javac)? The following is how my source code looks like so far:

import U; // This line marks an error.

public class DemoU
{
    public static void main(String[] args)
    {
        U u = new U();

        String[] myJavaFiles = u.system("dir");
        u.displayArray(myJavaFiles);
    }
}

I've placed both files, DemoU.java and U.class, in the same folder, and I try to compile everything using javac DemoU.java.

4
  • 2
    Add . to your classpath. Commented Sep 15, 2015 at 2:52
  • 2
    "-cp <class search path of directories and zip/jar files>" Commented Sep 15, 2015 at 2:52
  • remove import, you don't need it as both classes in the same folder Commented Sep 15, 2015 at 2:57
  • Ask your professor to provide that shared library as a JAR, instead of simply a .class file. Then just put that JAR in your classpath when you are building/executing. Done!. Simply providing a class file can be tricky, coz you don't even know which package that class is supposed to be! You need to put it in a correct directory structure if it is not in default package. Commented Sep 17, 2015 at 3:14

1 Answer 1

4

Remove import U;. You don't need it because both files are in the same location. That is what causing the problem.

Sign up to request clarification or add additional context in comments.

1 Comment

The OP says "I've placed both files, DemoU.java and U.class, in the same folder, and I try to compile everything using javac DemoU.java." and you're suggesting that he do exactly what he already does. That doesn't seem very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.