Including all the jars in a directory within the Java classpath

2021-6-3 anglehua

Is there a way to include all the jar files within a directory in the classpath? I'm trying java -classpath lib/*.jar:. my.package.Program and it is not able to find class files that are certainly in those jars. Do I need to add each jar file to the classpath separately? Using Java 6 or later, th...

阅读全文>>

评论(0) 浏览(246)

What is the difference between canonical name, simple name and class name in Java Class?

2021-6-3 anglehua

In Java, what is the difference between these: Object o1 = .... o1.getClass().getSimpleName(); o1.getClass().getName(); o1.getClass().getCanonicalName(); I have checked the Javadoc multiple times and yet this never explains it well. I also ran a test and that didn't reflect any real meaning behind ...

阅读全文>>

评论(0) 浏览(275)

How can I generate an MD5 hash?

2021-6-3 anglehua

Is there any method to generate MD5 hash of a string in Java? You need java.security.MessageDigest. Call MessageDigest.getInstance("MD5") to get a MD5 instance of MessageDigest you can use. The compute the hash by doing one of: Feed the entire input as a byte[] and calculate the hash in one operat...

阅读全文>>

评论(0) 浏览(255)

What does 'synchronized' mean?

2021-6-3 anglehua

I have some questions regarding the usage and significance of the synchronized keyword. What is the significance of the synchronized keyword? When should methods be synchronized? What does it mean programmatically and logically? The synchronized keyword is all about different threads reading and...

阅读全文>>

评论(0) 浏览(237)

How to mock void methods with Mockito

2021-6-3 anglehua

How to mock methods with void return type? I implemented an observer pattern but I can't mock it with Mockito because I don't know how. And I tried to find an example on the Internet but didn't succeed. My class looks like this: public class World { List<Listener> listeners; void ad...

阅读全文>>

评论(0) 浏览(237)

How can I get the current stack trace in Java?

2021-6-3 anglehua

How do I get the current stack trace in Java, like how in .NET you can do Environment.StackTrace? I found Thread.dumpStack() but it is not what I want - I want to get the stack trace back, not print it out. You can use Thread.currentThread().getStackTrace(). That returns an array of StackTraceEleme...

阅读全文>>

评论(0) 浏览(193)

Why can't I use switch statement on a String?

2021-6-3 anglehua

Is this functionality going to be put into a later Java version? Can someone explain why I can't do this, as in, the technical way Java's switch statement works? Switch statements with String cases have been implemented in Java SE 7, at least 16 years after they were first requested. A clear reason...

阅读全文>>

评论(0) 浏览(237)

:: (double colon) operator in Java 8

2021-6-3 anglehua

I was exploring the Java 8 source and found this particular part of code very surprising: //defined in IntPipeline.java @Override public final OptionalInt reduce(IntBinaryOperator op) { return evaluate(ReduceOps.makeInt(op)); } @Override public final OptionalInt max() { return reduce(Math::...

阅读全文>>

评论(0) 浏览(236)

Static Classes In Java

2021-6-3 anglehua

Is there anything like static class in java? What is the meaning of such a class. Do all the methods of the static class need to be static too? Is it required the other way round, that if a class contains all the static methods, shall the class be static too? What are static classes good for? Java...

阅读全文>>

评论(0) 浏览(215)

Difference between HashMap, LinkedHashMap and TreeMap

2021-6-3 anglehua

What is the difference between HashMap, LinkedHashMap and TreeMap in Java? I don't see any difference in the output as all the three has keySet and values. What are Hashtables? Map m1 = new HashMap(); m1.put("map", "HashMap"); m1.put("schildt", "java2"); m1.put("mathew", "Hyden"); m1.put("schildt",...

阅读全文>>

评论(0) 浏览(230)

Why doesn't RecyclerView have onItemClickListener()?

2021-6-3 anglehua

I was exploring RecyclerView and I was surprised to see that RecyclerView does not have onItemClickListener(). I've two question. Main Question I want to know why Google removed onItemClickListener()? Is there a performance issue or something else? Secondary Question I solved my problem by writing ...

阅读全文>>

评论(0) 浏览(252)

StringBuilder vs String concatenation in toString() in Java

2021-6-3 anglehua

Given the 2 toString() implementations below, which one is preferred: public String toString(){ return "{a:"+ a + ", b:" + b + ", c: " + c +"}"; } or public String toString(){ StringBuilder sb = new StringBuilder(100); return sb.append("{a:").append(a) .append(", b:").append(b...

阅读全文>>

评论(0) 浏览(241)

Can't start Eclipse - Java was started but returned exit code=13

2021-6-3 anglehua

I am trying to get my first taste of Android development using Eclipse. I ran into this problem when trying to run Eclipse, having installed version 4.2 only minutes ago. After first trying to start Eclipse without any parameters to specify the Java VM, I got an error message saying it couldn't find...

阅读全文>>

评论(0) 浏览(254)

How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

2021-6-3 anglehua

I have some code that uses JAXB API classes which have been provided as a part of the JDK in Java 6/7/8. When I run the same code with Java 9, at runtime I get errors indicating that JAXB classes can not be found. The JAXB classes have been provided as a part of the JDK since Java 6, so why can Jav...

阅读全文>>

评论(0) 浏览(232)

Java 8 List<V> into Map<K, V>

2021-6-3 anglehua

I want to translate a List of objects into a Map using Java 8's streams and lambdas. This is how I would write it in Java 7 and below. private Map<String, Choice> nameMap(List<Choice> choices) { final Map<String, Choice> hashMap = new HashMap<>(); for (final C...

阅读全文>>

评论(0) 浏览(241)

What is the difference between JDK and JRE?

2021-6-3 anglehua

What is the difference between JDK and JRE? What are their roles and when should I use one or the other? The JRE is the Java Runtime Environment. It is a package of everything necessary to run a compiled Java program, including the Java Virtual Machine (JVM), the Java Class Library, the java comman...

阅读全文>>

评论(0) 浏览(234)

Reading a plain text file in Java

2021-6-3 anglehua

It seems there are different ways to read and write data of files in Java. I want to read ASCII data from a file. What are the possible ways and their differences? ASCII is a TEXT file so you would use Readers for reading. Java also supports reading from a binary file using InputStreams. If the fil...

阅读全文>>

评论(0) 浏览(206)

When to use static methods

2021-6-3 anglehua

I am wondering when to use static methods? Say if I have a class with a few getters and setters, a method or two, and I want those methods only to be invokable on an instance object of the class. Does this mean I should use a static method? Example: Obj x = new Obj(); x.someMethod(); ...or: Obj.som...

阅读全文>>

评论(0) 浏览(203)

How to set or change the default Java (JDK) version on macOS?

2021-6-3 anglehua

How can you change the default version of Java on a mac? First run /usr/libexec/java_home -V which will output something like the following: Matching Java Virtual Machines (3): 1.8.0_05, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home 1.6.0_65-b14-462, x86_64: ...

阅读全文>>

评论(0) 浏览(233)

Place cursor at the end of text in EditText

2021-6-3 anglehua

I am changing the value of an EditText on keyListener. But when I change the text the cursor is moving to the beginning of the EditText. I need the cursor to be at the end of the text. How to move the cursor to the end of the text in a EditText. Try this: UPDATE: Kotlin: editText.setSelection(editT...

阅读全文>>

评论(0) 浏览(215)

Powered by emlog 京ICP备15036472号-3 sitemap