How can I create an executable JAR with dependencies using Maven? I want to package my project in a single executable JAR for distribution. How can I make Maven package all dependency JARs into my JAR? Answer: <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>fully.qualified.MainClass</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build> and you run it with mvn clean compile assembly:single Compile goal should be added before assembly:single or otherwise the code on your own project is not included. See more
undefined
Initialization of an ArrayList in one line I want to create a list of options for testing purposes. At first, I did this: ArrayList<String> places = new ArrayList<String>(); places.add("Buenos Aires"); places.add("Córdoba"); places.add("La Plata"); Then I refactored the code as follows: ArrayList<String> places = new ArrayList<String>( Arrays.asList("Buenos Aires", "Córdoba", "La Plata")); Is there a better way to do this? Answer: Actually, probably the "best" way to initialize the ArrayList is the method you wrote, as it does not need to create a new List in any way: ArrayList<String>
undefined
How do I test a class that has private methods, fields or inner classes? How do I use JUnit to test a class that has internal private methods, fields or nested classes? It seems bad to change the access modifier for a method just to be able to run a test. Solution: If you have somewhat of a legacy application, and you're not allowed to change the visibility of your methods, the best way to test private methods is to use reflection. Internally we're
undefined
In Java, difference between default, public, protected, and private In Java, are there clear rules on when to use each of access modifiers, namely the default (package private), public, protected and private, while making class and interface and dealing with inheritance? Answer: The official tutorial may be of some use to you. | Class | Package | Subclass | Subclass | World | | |(same pkg)|(diff pkg)| ————————————+———————+—————————+——————————+——————————+———————— public | + | + | + | + | + ————————————+———————+—————————+——————————+——————————+———————— protected | + | + | + | + | o ————————————+———————+—————————+——————————+——————————+————————
undefined
How to efficiently iterate over each Entry in a Map? If I have an object implementing the Map interface in Java and I wish to iterate over every pair contained within it, what is the most efficient way of going through the map? Will the ordering of elements depend on the specific map implementation that I have for the interface? Solution: Map<String, String> map = ... for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey() + "/" + entry.getValue()); } http://stackoverflow.com/questions/46898/how-to-efficiently-iterate-over-each-entry-in-a-map
undefined
Is there a unique Android device ID? Do Android devices have a unique ID, and if so, what is a simple way to access it using Java? Solution : Settings.Secure#ANDROID_ID returns the Android ID as an unique for each user 64-bit hex string. import android.provider.Settings.Secure; private String android_id = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID); Update: 06/05/2015 When reviewing this as a possible solution, please consider Joe's answer below instead as an alternative. http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id
undefined
How to convert a String to an int in Java? How can I convert a String to an int in Java? My String contains only numbers and I want to return the number it represents. For example, given the string "1234" the result should be the number 1234. Solution: int foo = Integer.parseInt("1234"); See the Java Documentation for more information. (If you have it in a StringBuilder (or the ancient StringBuffer), you'll need to do Integer.parseInt(myBuilderOrBuffer.toString()); instead). http://stackoverflow.com/questions/5585779/how-to-convert-a-string-to-an-int-in-java