0

I downloaded an external library, common-codecs, and am trying to create a package from the downloaded source code so that i can import and use it in java class files. How would i go about doing this?

I moved the downloaded directory into the same directory as my java class files.

What I've tried so far:

    package commons-codec-1.11-src;

I place this at the head of my java class file

Then i try and compile the file using javac in the Linux terminal

    javac -cp ~/Documents/javapractice/commons-codec-1.11-src ~/Documents/javapractice/File.java

I get a "interface, class, or enum required error" and the compiler error points to the package statement in the java class file.

code:

    import java.util.*
    package commons-codec-1.11-src;

    public class File
    {
     ........

    }

Just to clear things up commons-codec-1.11-src is source code I downloaded and is now a directory in the same directory as File.java Any help would be greatly appreciated! Thank You!

1
  • Is there a reason you're building the package instead of just using the jar file and a dependency management tool such as Maven or Gradle? Commented Jan 21, 2018 at 1:10

1 Answer 1

1

I downloaded an external library, common-codecs, and am trying to create a package from the downloaded source code so that i can import and use it in java class files. How would i go about doing this?

You don't need and you should not package the source code of the external library in your application.
Extracting dependency classes in your own application is a very corner use case and it should done only as you have no choice.

What you need is adding the jar that contains the compiled classes in your classpath at compilation (javac command) and at runtime (java command).

Supposing that the jar is named commons-codec-1.11.jar, to compile your File.java class you should execute :

javac -cp ~/Documents/javapractice/commons-codec-1.11.jar /~/Documents/javapractice/File.java

The File.java declaration is not correct either.

The package declaration has to happen before the import declaration and the package and import values are not correct either.

It should be something as :

package javapractice;
import java.util.*;

public class File {
 ........

}

About import from the third party library, you need to import classes you use in File class.
You cannot import the whole package as you try to do.

I think that you should try to understand javac/java bases and start with an IDE to make things easier.

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

Comments

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.