Showing posts with label java-core. Show all posts
Showing posts with label java-core. Show all posts

Monday, April 18, 2016

Where is Java home on Mac OS X?

I'm using Mac OS X Lion v10.7 and I have downloaded Java 8 DMG file from Oracle's web site and installed it. After installing it, i can't find Java home directory. According to http://stackoverflow.com/questions/1348842/what-should-i-set-java-home-to-on-osx, there is an executable file that print Java home location to console by default, /usr/libexec/java_home.

> ls -l /usr/libexec/java_home
lrwxr-xr-x  1 root  wheel  79 Apr  7 17:03 /usr/libexec/java_home -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home
> /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home

And then you can set JAVA_HOME variable in /etc/profile file as following:
> export JAVA_HOME=$(/usr/libexec/java_home)


Monday, January 5, 2015

Guessing file content type

You can get file content type from a URL as following:

String fileUrl = "http://example.com/myimage.jpg";
String contentType = URLConnection.guessContentTypeFromName(fileUrl);

and the result would be:

image/jpeg

There is another method to guess file content type from input stream instead, URLConnection.guessContentTypeFromStream(InputStream) but if the server does not return content type of the file in the response header when you access it, the method would return null. Guessing content type from file name still work in this case.


Saturday, February 16, 2013

Compiling and running Java program

Suppose we have Hello.java file in the folder D:/myprogram/com/example/ and the package of the Hello class is com.example.



Compiling

D:> javac myprogram\com\example\Hello.java
and then the Hello.class file will be created in the same folder as Hello.java.

If your program is using third-party libraries and they are in D:\myprogram\libs, then:
D:> javac -cp "D:\myprogram\libs" myprogram\com\example\Hello.java



Running

D:> java -cp "D:\myprogram" com.example.Hello

If there are third-party libraries in D:\myprogram\libs folder, then:
D:> java -cp "D:\myprogram\libs\*;D:\myprogram" com.example.Hello