releseabe
releseabe

Reputation: 329

downloading jar file from github?

I have committed a 10 meg jar file to a git repo and i would like to download the file using Java. The file can be downloaded from github directly but when I attempt to curl it from the command line (as an experiment -- I definitely need to do this from a program) two things: 1. It appears to proceed too fast -- I don't think I am getting the actual jar. 2. I think I am getting HTML. I know about "raw" for source files but there is no raw option offered on Github for binaries apparently.

In the program what I was able to do with source source was read the download 1 line at a time and append those lines, eventually recreating the file. Not sure how this will work with a large jar file.

EDIT: There is a "view raw" option which appends ?raw=true to the URL but that is not working from Java and when I try from command line, it does not seem to work -- still getting html and download goes too fast.

EDIT: Here is the command line curl of a jar file that is present in github: curl -u testuser https://github.com/test/test-api/blob/master/testjarfile.jar?raw=true

The above produces results but I dont think it is what we need. Here is the Java code:

URL url;
    String username="testuser";
    String password= "testpass";
    StringBuilder file = new StringBuilder();//deliberately not thread-safe
    try {
        url = new URL("https://github.com/test/test-api/blob/master/testjarfile.jar?raw=true");
        URLConnection uc;
        uc = url.openConnection();

        uc.setRequestProperty("X-Requested-With", "Curl");
        String userpass = username + ":" + password;
        String basicAuth = "Basic " + new String(Base64.getEncoder().encodeToString(userpass.getBytes()));//needs Base64 encoder, apache.commons.codec
        uc.setRequestProperty("Authorization", basicAuth);

        BufferedReader reader = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        String line = null;
        while ((line = reader.readLine()) != null) 
            file.append(line+"\n");
        System.out.println(file);

And this Java code gives file not found. Note that from the github webpage, it appears that the jarfile is actually downloaded. Note also the "raw=true" which I would have guessed would have caused the raw file to be be downloaded, not html, etc.

Upvotes: 1

Views: 2450

Answers (1)

VonC
VonC

Reputation: 1323773

Try first the curl command described in "How can I download a single raw file from a private github repo using the command line?", to check the curl itself works from command-line.

curl -H 'Authorization: token YOUR_TOKEN' \
  -H 'Accept: application/vnd.github.v4.raw' \
  -O \
  -L https://api.github.com/repos/INSERT_OWNER_HERE/INSERT_REPO_HERE/contents/PATH/TO/FILE

Then use rockswang/java-curl for instance to translate that curl call into Java.

Upvotes: 1

Related Questions