1

I am new in Java programming language and i want to use a library by importing their packages . Can anyone tell me how can i import packages in Java using text editor? I found this library in github and i wanted to use their packages for my java code i am developing by using import. I tried just to call these packages on my code by using import but in compiler there was an error which states: packages not found.

import com.tiemens.secretshare.main.cli.*;
import com.tiemens.secretshare.main.cli.*;

import java.io.*;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.Integer.min;
import static java.util.Arrays.copyOfRange;


public class Shamir {

    //The encoding that will be used when splitting and combining files.
    static String encoding = "ISO-8859-1";
    //The number of bytes per piece (except maybe the last one)!
    static int pieceSize = 128;

    //Mode 0 for strings, 1 for ints.
    public static ArrayList<String> shamirSplit(String inputString, int numPieces, int minPieces, int mode) {

        String type = "-sS";
        if (mode == 1) {
            type = "-sN";
        }

        ArrayList<String> parts = new ArrayList<>();
        String[] splitArgs = {"-n", Integer.toString(numPieces), "-k", Integer.toString(minPieces), type, inputString, "-primeNone"};
        MainSplit.SplitInput splitInput = MainSplit.SplitInput.parse(splitArgs);
        MainSplit.SplitOutput splitOutput = splitInput.output();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        splitOutput.print(ps);
        String content = baos.toString(); // e.g. ISO-8859-1
        BufferedReader reader = new BufferedReader(new StringReader(content));
        String line;
        int i = 0;
        try {
            while ((line = reader.readLine()) != null && i < numPieces) {
                if (line.startsWith("Share (x")) {
                    i++;
                    parts.add(line.trim());
                }
            }
        } catch (Exception e) 

So my class i want to implement is Shamir class but i need to import com.tiemens.secretshare.main.cli.*;

Can anyone tell me how to make this package work for my Shamir class?

john smith
  • 21
  • 6

3 Answers3

3

I am guessing you aren't using maven. Download the jar files for packages you want to import and put then in your build path

Charu Khurana
  • 4,511
  • 8
  • 47
  • 81
0

If I am not mistaken, when I do what you did:

import com.tiemens.secretshare.main.cli.*;
public class Foo {

}

and then try to compile using javac Foo.java, I get:

Error:(2, 1) java: package com.tiemens.secretshare.main.cli does not exist

This means that when the compiler javac is trying to compile your class (Shamir.java) it needs either the source files or the bytecode (class files) for the classes in the package com.tiemens.secretshare.main.cli. Since you seem to have neither, the compilation fails.

Thus, you need the jar file that contains the classes they you want to compile against. There are two ways to achieve this:

  1. Use Maven. But that means you need to learn Maven. That's life. Use mvn repo to compile against.
  2. If you think it is too much of work to learn Maven, you will need to build secretshare code on GitHub yourself. This means you will need to learn gradle. Again, that's life.

Too bad you couldn't download the JAR file as a "release download" for this project.

Kedar Mhaswade
  • 4,535
  • 2
  • 25
  • 34
0

Download the jar for your library and include it in your project classpath. Then you can import it in your class. For setting the classpath use this link

Milind Gokhale
  • 575
  • 2
  • 14
  • the link you provide is not working . Can you post the link again please. Thank you for your comment – john smith Mar 03 '16 at 01:15
  • updated the link. Basically the link is about how to configure classpath in eclipse. You can use the link or search for any link to configure classpath. – Milind Gokhale Mar 05 '16 at 19:13