background preloader

Tecnic

Facebook Twitter

Ke CAI: Find and Kill Running Windows Processes in Java. One of my friends asked me if it is possible to check a programming is running or not in Windows using Java. And close it if it is running. After a little bit research, I found the Java solution for his requirement. The key is Runtime.getRuntime().exec(), you can run commands available in this platform. In Windows, the "tasklist" command will show you all the processes running (In Linux, you might want to look at "ps"). tasklist The result of this command looks like this: Here is the code to iterate and find running process: When you make sure the enemy process is runing, you can use "taskkill /IM xxx.exe" to kill it.

Taskkill -IM WINWORD.EXE Below is the code to kill process by name: Let's have a look how to use this process helper: Steganography - Java Tutorials. A Touch of Functional Style in Plain Java with Predicates – Part 1. We Recommend These Resources You keep hearing about functional programming that is going to take over the world, and you are still stuck to plain Java? Fear not, since you can already add a touch of functional style into your daily Java. In addition, it’s fun, saves you many lines of code and leads to fewer bugs. What is a predicate?

I actually fell in love with predicates when I first discovered Apache Commons Collections, long ago when I was coding in Java 1.4. 1.evaluate(Object object): boolean That’s it, it just takes some object and returns true or false. 1.apply(T input): boolean It is that simple. A simple example As an early example, imagine you have a list orders of PurchaseOrder objects, each with a date, a Customer and a state. 01.public List<PurchaseOrder> listOrdersByCustomer(Customer customer) { 02. final List<PurchaseOrder> selection = new ArrayList<PurchaseOrder>(); 03. for (PurchaseOrder order : orders) { 04. if (order.getCustomer().equals(customer)) { 05. selection.add(order);

Internationalization in JSF with UTF-8 encoded properties files | J-Development. Introduction To internationalize your (web)application, the normal approach is to use the ResourceBundle API in combination with properties files which contains externalized text. The ResourceBundle API will load the proper text based on the current Locale and the default (fallback) locale.

This sounds nicer than it actually is. Deep under the covers, the ResourceBundle API uses Properties#load(InputStream) method to load the properties files. This method uses by default the ISO-8859-1 encoding. This is explicitly mentioned in its javadoc as well. The load(InputStream) / store(OutputStream, String) methods work the same way as the load(Reader)/store(Writer, String) pair, except the input/output stream is encoded in ISO 8859-1 character encoding. In a nutshell, you can’t have something like the following as UTF-8 in your properties files: some.dutch.text = Één van de wijken van Curaçao heet Saliña. When incorrectly decoded using ISO-8859-1, it would end up as mojibake: Ã? The old solution. Hierarchical structures with Java Enums. Java enums are typically used to hold array like data. This tip shows how to use enum for hierarchical structures. Motivation Once upon a time I wanted to create enum that contains various operating system, i.e. public enum OsType { WindowsNTWorkstation, WindowsNTServer, Windows2000Server, Windows2000Workstation, WindowsXp, WindowsVista, Windows7, Windows95, Windows98, Fedora, Ubuntu, Knopix, SunOs, HpUx, I was not satisfied of this structure because I'd like to see a group of WindowsNT that contains WinNTWorkstation and WindNT server.

Class per OS Solution The obvious solution here is to create separate classes per operating system and several abstract classes. But now we cannot see all operating systems together, iterate over them etc., i.e. very useful features of Java enum are missing. Fedora(Fedora.class), Ubuntu(Ubuntu.class), Knopix(Knopix.class), SunOs(SunOs.class), HpUx(HpUx.class), private Class clazz; OsType(Class clazz) { this.clazz = clazz; Overriding parent's method The problem is solved. An Array of Reasons to not use Arrays in Java | Drawing a Blank. This post is meant for the novice java developer who is either learning java as their first programming language or is coming from another language that's a bit lower level than java - perhaps C. One of the most common things I see developers new to java do when they first need to have "collections" of things in their java applications is to resort to arrays to get the job done. The new folks are either choosing arrays when they shouldn't either out of habit or out of ignorance; or in the case of students, their instructor is probably forcing them to.

If the latter is true for you, then listen to your instructor and tuck away this link for future reading since you, your legal guardians, or some scholarship/grant are paying for your instructors tuteledge. If not, read on... What is wrong with Arrays? Arrays in java have several problems which should dissuade you from ever using them except in a select few situations. Arrays have a fixed size Arrays are impossible to make immutable. Have a simple HTTP server. Have a simple HTTP serverTag(s): Networking Since Java 1.6, there's a built-in HTTP server included with the JDK.

The HttpServer provides a simple high-level Http server API, which can be used to build embedded HTTP servers. Compile and execute. To access the local server, open a browser at The next HttpServer provides 2 contexts : How to have a simple HTTP server in Java. Have a simple HTTP serverTag(s): Networking Since Java 1.6, there's a built-in HTTP server included with the JDK. The HttpServer provides a simple high-level Http server API, which can be used to build embedded HTTP servers.

Compile and execute. To access the local server, open a browser at The next HttpServer provides 2 contexts : Software Development Thoughts: Synthetic Composite Types with Java Generics. Perhaps everyone already knows about this, but I just found a very interesting feature of Java Generics - you can declare synthetic composite types using multiple bounds to allow you to treat two different classes that implement the same interfaces as if they had a common super type, even if they don't.

Consider the following classes: Now imagine that you want to write a method that depends on taking items that are both FooInterface and BarInterface, but beyond that does not care about their types. It should accept both instances of Foo and Bar. In the past I'd have cursed the fact that Foo and Bar have no common supertype capturing being both FooInterface and BarInterface, but I now know you can declare a method as so: However, what I haven't yet worked out how to do is use a method that can take a Collection of that synthetic type, as so: The method declaration works, but how to call it?

Can anyone help? Edit: found a clunky way to do it: makeCollection. Java Code Geeks: Java Best Practices – Queue battle and the Linked ConcurrentHashMap. Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to perform a performance comparison between four popular Queue implementation classes with relevant semantics. To make things more realistic we are going to test against a multi–threading environment so as to discuss and demonstrate how to utilize ArrayBlockingQueue, ConcurrentLinkedQueue, LinkedBlockingQueue and/or LinkedList for high performance applications.

Last but not least we are going to provide our own implementation of a ConcurrentHashMap. The ConcurrentLinkedHashMap implementation differs from ConcurrentHashMap in that it maintains a doubly–linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map. All tests are performed against a Sony Vaio with the following characteristics : The following test configuration is applied : Test case #3 – Iterators. MF Bliki: AnemicDomainModel. Bad things · domain driven design · application architecture tags: This is one of those anti-patterns that's been around for quite a long time, yet seems to be having a particular spurt at the moment. I was chatting with Eric Evans on this, and we've both noticed they seem to be getting more popular. As great boosters of a proper Domain Model, this is not a good thing. The basic symptom of an Anemic Domain Model is that at first blush it looks like the real thing.

The fundamental horror of this anti-pattern is that it's so contrary to the basic idea of object-oriented design; which is to combine data and process together. Now object-oriented purism is all very well, but I realize that I need more fundamental arguments against this anemia. It's also worth emphasizing that putting behavior into the domain objects should not contradict the solid approach of using layering to separate domain logic from such things as persistence and presentation responsibilities.

Code Monkeyism: Never, never, never use String in Java (or at least less often :-) May 2, 2008 by Stephan Schmidt Never, never, never use (unwrapped) String or long or int. Why? Those primitive types have no semantic meaning. They are hard to understand, hard to maintain, and hard to extend. I’ve been evangelizing this concept for some time, the essay “Object calisthenics” finally prompted be to write this post. Suppose we have an example of a cinema ticket booking service. Update: If you just want to drop a comment telling me how revolting you find the idea, well, just don’t. Compare public void bookTicket( String name, String firstName, String film, int count, String cinema); with (and I know one would introduce an Order object for real code): public void bookTicket( Name name, FirstName firstName, Film film, Count count, Cinema cinema); void book(String orderId); with void book(OrderId order); In the first case the developer seeing the code wonders a.) where to get an orderId and b.) what an orderId really is, "1212", "ABC-123" or "12-34-45-SCHWEINEBACKE".

About the author. Repeat after me: Immutable objects will not slow you down. As noted by Stephan Schmidt the advent of functional programming promotes the use of immutable objects even in non-functional languages. Immutable objects have many positive traits: they are safer, they throw less exception, they are not prone to problems of covariance, they prevent races when used in a multi-threaded settings, etc. The main (only?) Down side of immutable object is the fact that they make it difficult to update the data in your program. This should not be a surprise. After all, if they are immutable then it only makes sense that they will not encourage mutations. Here is the general description of the problem: if x is an immutable object and x.f == y, and you want x.f to point at z, then you can't just set x.y to z.

In certain cases (such as: many object holding references to x) this rerouting may be quite hard and error prone. Reason #1. Reason #2. Still not convinced? Code Monkeyism: Go Ahead: Next Generation Java Programming Style. August 10, 2009 by Stephan Schmidt Many companies and developers move away from Java to new languages: Ruby, Python, Groovy, Erlang, Scala. You might be trapped with Java. Even if you’ve trapped, you can change your programming style and reap some of the benefits of those new languages. In the last 15 years Java programming style has changed significantly: This tips lead to better Java code. Uodate: Some thoughts to Cedrics thoughts. As the author of the post, your thoughts are appreciated, some of mine: “Besides, it’s convenient to be able to mutate object if you want to use pools.” No setters doesn’t mean you can’t mutate objects, it’s just that plain setters are not object oriented thinking. “I think the first example is more readable than the second one that uses Predicates.”

I think your assumptions are wrong. About the author Stephan Schmidt has been working with internet technologies for the last 20 years.