0

I have some piece of code:

package bk;

import java.util.Vector;
import java.util.Scanner;

public class bk {
    private static int iAsLoopRunner=0;
    static Scanner takeInput=new Scanner(System.in);
    public static void print_int(int arr[])
    {
        for(iAsLoopRunner=0;iAsLoopRunner<arr.length;iAsLoopRunner++)
        {
            System.out.print(arr[iAsLoopRunner]+" "+"|"+" ");
        }
        System.out.println();
    }
    public static void print_float(float arr[])
    {
        for(iAsLoopRunner=0;iAsLoopRunner<arr.length;iAsLoopRunner++)
        {
            System.out.print(arr[iAsLoopRunner]+" "+"|"+" ");
        }
        System.out.println();
    }
    public static void print_string(String arr[])
    {
        for(iAsLoopRunner=0;iAsLoopRunner<arr.length;iAsLoopRunner++)
        {
            System.out.print(arr[iAsLoopRunner]+" "+"|"+" ");
        }
        System.out.println();
    }
    public static void print_double(double arr[])
    {
        for(iAsLoopRunner=0;iAsLoopRunner<arr.length;iAsLoopRunner++)
        {
            System.out.print(arr[iAsLoopRunner]+" "+"|"+" ");
        }
        System.out.println();
    }
    public static void print_long(long arr[])
    {
        for(iAsLoopRunner=0;iAsLoopRunner<arr.length;iAsLoopRunner++)
        {
            System.out.print(arr[iAsLoopRunner]+" "+"|"+" ");
        }
        System.out.println();
    }
    public static void print_vector(Vector arr)
    {
        for(iAsLoopRunner=0;iAsLoopRunner<arr.size();iAsLoopRunner++)
        {
            System.out.print(arr.get(iAsLoopRunner)+" "+"|"+" ");
        }
        System.out.println();
    }
    public static void sop(Object obj)
    {
        System.out.print(obj+" ");
    }
    public static void sopln(Object obj)
    {
        System.out.println(obj);
    }
    public static int[] getInt(int arraySize)
    {
        int arrayToReturn[]=new int[arraySize];
        for(iAsLoopRunner=0;iAsLoopRunner<arraySize;iAsLoopRunner++)
        {
            arrayToReturn[iAsLoopRunner]=takeInput.nextInt();
        }
        return arrayToReturn;
    }
    public static String[] getString(int arraySize)
    {
        String arrayToReturn[]=new String[arraySize];
        for(iAsLoopRunner=0;iAsLoopRunner<arraySize;iAsLoopRunner++)
        {
            arrayToReturn[iAsLoopRunner]=takeInput.next();
        }
        return arrayToReturn;
    }
    public static float[] getFloat(int arraySize)
    {
        float arrayToReturn[]=new float[arraySize];
        for(iAsLoopRunner=0;iAsLoopRunner<arraySize;iAsLoopRunner++)
        {
            arrayToReturn[iAsLoopRunner]=takeInput.nextFloat();
        }
        print_float(arrayToReturn);
        System.out.println("done ;)");
        return arrayToReturn;
    }
    public static long[] getLong(int arraySize)
    {
        long arrayToReturn[]=new long[arraySize];
        for(iAsLoopRunner=0;iAsLoopRunner<arraySize;iAsLoopRunner++)
        {
            arrayToReturn[iAsLoopRunner]=takeInput.nextLong();
        }
        print_long(arrayToReturn);
        System.out.println("done ;)");
        return arrayToReturn;
    }
    public static double[] getDouble(int arraySize)
    {
        double arrayToReturn[]=new double[arraySize];
        for(iAsLoopRunner=0;iAsLoopRunner<arraySize;iAsLoopRunner++)
        {
            arrayToReturn[iAsLoopRunner]=takeInput.nextDouble();
        }
        print_double(arrayToReturn);
        System.out.println("done ;)");
        return arrayToReturn;
    }
}

As one can see that this simplifies many lines of code like printing array just with one function.This can be greatly useful if we want to see how our array is changing while undergoing a sorting process.But for each project I have to create a new package and import this.It takes some time.Is there any way so that I can inject this package to java root packages like util package or whatever.I have tried a lot in google but every one finally shown creating a jar and importing that to project which is same as what I'm doing now.Thanks in advance :)

This is the error I'm getting

Community
  • 1
  • 1
Bharat
  • 1,044
  • 15
  • 34

2 Answers2

1

You may want to set the environment variable CLASSPATH (for example export CLASSPATH=/path/to/your.jar) to have your jar in the class path in your environment.

Knut Forkalsrud
  • 1,134
  • 1
  • 7
  • 19
1

You have to add the jar to your dependencies folder, with any external library. With third-party tools you can do this with a package management tool like Gradle or Maven, and you could use those tools as well if you host your package online, but most likely I believe in your case you're best off just pasting it in the folder. This really shouldn't be a time-intensive process as long as you're working on a manageable number of projects.

RaceYouAnytime
  • 709
  • 5
  • 16
  • bro I code always.I used to solve lot of problems in hackerrank and homework assignments. Its becoming bit difficult to paste it every time.Please explain clearly or give a link so I would follow that link – Bharat Apr 25 '17 at 17:59
  • What IDE do you use? – RaceYouAnytime Apr 25 '17 at 18:01
  • Now I'm using intellij idea for java.Its just same as android studio – Bharat Apr 25 '17 at 18:02
  • @bharath take a look at this post: http://stackoverflow.com/questions/1051640/correct-way-to-add-external-jars-lib-jar-to-an-intellij-idea-project – RaceYouAnytime Apr 25 '17 at 18:07
  • but this is same as adding jars which I knew already.As I said I want to inject into core java packages so that I can freely use the methods irrespective of the ide. Just like I want to create a new class in util package.So I can simply use these methods by saying import java.util.bk; – Bharat Apr 25 '17 at 18:11
  • My question is related to Knut Forkalsrud's answer.Please think in that way. – Bharat Apr 25 '17 at 18:12
  • @bharath, ahh, I see. Take a look at this tutorial then. Should be exactly what you want: http://www.avajava.com/tutorials/lessons/how-do-i-update-my-classpath-to-include-a-jar-file.html – RaceYouAnytime Apr 25 '17 at 18:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142652/discussion-between-bharath-and-raceyouanytime). – Bharat Apr 25 '17 at 18:24
  • Hmm I tried it bro. It worked for current project and its not working for other projects...:( – Bharat Apr 25 '17 at 18:39