Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Monday, January 30, 2012

ProcessBuilder Runtime Redirecting Output and Exec uting

The following discuss details of the problem:


ProcessBuilder redirecting output


http://stackoverflow.com/questions/5986324/processbuilder-redirecting-output

Shell redirection operators are unknown to ProcessBuilder. Put your command in a shell script and execute it, as shown here. Alternatively, use bash -c, as shown here.



Or prepending "bash -c" to the exec


http://stackoverflow.com/questions/5740390/printing-my-macs-serial-number-in-java-using-unix-commands/5740673#5740673


How to use OutputStream for output redirection


http://stackoverflow.com/questions/3777654/problem-with-runtime


Split a string containing command-line parameters into a String[] in Java


http://stackoverflow.com/questions/3259143/split-a-string-containing-command-line-parameters-into-a-string-in-java
Wonderful answer here:


Here is a pretty easy alternative for splitting a text line from a file into an argument vector so that you can feed it into your options parser:
This is the solution:
public static void main(String[] args) {
    String myArgs = Commandline.translateCommandline("-a hello -b world -c \"Hello world\"");
    for (String arg:myArgs)
        System.out.println(arg);
}

Friday, January 27, 2012

Monday, December 12, 2011

getResouce and Files in java. I just hate having to search for this every time.

To get a file inside a jar, you have to do the following:

URI resourceURI = this.class.getResource("/path/to/file/in/jar/config.txt").toURI();

To open the file using a file you can do:

File theFile = new File(resourceURI);

To read the contents of the file:

List<String> lines = Files.readLines(theFile, Charsets.UTF_8);


To read the files in a directory:



// Load the directory as a resource
URL dir_url = ClassLoader.getSystemResource(dir_path);
// Turn the resource into a File object
File dir = new File(dir_url.toURI());
// List the directory
String files = dir.list()

Monday, October 31, 2011

"Run On Server" not showing up in Eclipse with m2eclipse?

Okay, I went through this problem in Eclipse Indigo for J2EE Developers release that m2eclipse wont show me the Run On Server selection in the Run menu when I import a maven project with war packaging (ie. the maven-archetype-webapp).

Here is how to fix it:
1. Uninstall the current m2eclipse plugin you have in eclipse:
  (a) Go to "Help > Install New Software"
  (b) Click on the "What is already installed?" link
  (c) Select "m2e - Maven Eclipse Integration" and click on the "Uninstall" button
  (d) Restart eclipse when prompted

2. Next, you have to install the m2eclipse:
  (a) Go to "Help > Eclipse Market Place"
  (b) Search for the word m2eclipse
  (c) Select "Maven integration for eclipse"
  (d) Click the "Install" button

3. Install the "Maven Integration for Eclipse WTP"

  (a) Go to "Help > Eclipse Market Place"
  (b) Search for the word m2eclipse
  (c) Select "Maven integration for eclipse WTP
  (d) Click the "Install" button

Restart when prompted.

Now, importing a maven project with war packaging will work out of the box. Enjoy.

Sunday, October 23, 2011

Configuring Eclipse to run with a specific JDK or JRE


1. Open the file eclipse.ini for editing

2. Add the two lines to specify the virtual machine to use (the bolded lines):

-startup
plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar

-vm 
C:\Program Files\Java\jdk1.6.0_29\bin\javaw.exe

--launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.100.v20110502

3. Now start eclipse.

Monday, October 17, 2011

Using mod_jk with Apache 2.2 How to setup a reverse proxy



Advice on mod_jk versus mod_proxy and mod_proxy_ajp
Do not use mod_proxy and mod_proxy_ajp on Apache 2.2 since they are not stable and have caused me some problems. Use mod_jk instead.

Prerequisites & Assumptions Before you continue
I assume you have:
- You already have Apache Webserver 2.2 installed
- You already have Tomcat installed
- You already have a web application on Tomcat with context "jobsapp"
- You are using Redhat Enterprise or CentOS and therefore have yum on your system.

How to get the mod_jk binary for Apache 2.2 on Linux
You wont find it anywhere, you have to compile it, now stop being lazy and start working.

The following steps detail the process:

// Create a temporary source directory, I use /opt/src
mkdir /opt/src
cd /opt/src 


// Get the source package
wget http://mirrors.isu.net.sa/pub/apache//tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.32-src.tar.gz


// Install packages needed to compile the binary
yum install httpd-devel
yum install gcc-c++


// Extract the tar.gz file
tar -zxvf /opt/src/tomcat-connectors-1.2.32-src.tar.gz


// Configure, make, make install
./configure --with-apxs=/usr/sbin/apxs
make
su -c 'make install'

How to configure Apache Webserver with Mod JK

1. Edit the file httpd.conf
vim /etc/httpd/conf/

2. Add this line to load the module:
...
LoadModule jk_module modules/mod_jk.so
...

3. Add also the following lines:

JkWorkersFile   conf/worker.properties
JkLogFile       /var/log/httpd/mod_jk.log
JkLogLevel      debug
JkRequestLogFormat      "%w %U %T"
JkOptions +ForwardURIEscaped +ForwardURICompatUnparsed

After finishing, change JkLogLevel from debug to "error" or "info" so that the log file doesn't grow too much in size.

4. Now create a file names worker.properties inside /etc/httpd/conf/ and add the following lines to it:
vim /etc/httpd/conf/worker.properties

# Worker Properties file for mod_jk
worker.list=tomcat
worker.tomcat.host=localhost
worker.tomcat.port=8009
worker.tomcat.type=ajp13
worker.tomcat.connection_pool_timeout=300




4.5. Make sure to read the mod_jk attributes in this link:
http://tomcat.apache.org/connectors-doc/reference/workers.html

5. Now open the httpd.conf or your virtual host configuration file and add the following lines:

JkMount     /jobsapp tomcat
JkMount     /jobsapp/* tomcat

6. Also open the ssl httpd conf file which has the following line at the top,
and add the same two lines inside them:

<VirtualHost _default_:443>
...
JkMount     /jobsapp tomcat
JkMount     /jobsapp/* tomcat
...
</VirtualHost>


7. Now restart apache

/etc/init.d/httpd restart

Monday, September 12, 2011

Automatic Java Class Diagram Generation Using ObjectAid

I was looking for a tool that would allow me to view my classes graphically, until I bumped into this question from stackoverflow.com:

http://stackoverflow.com/questions/1120032/what-is-your-favorite-automatic-class-diagram-generator-for-eclipse

And after trying several tools, I think ObjectAid beats'em all.

Object Aid Website:
http://www.objectaid.com/

Tuesday, February 13, 2007

The Problem With The PeePee's

With all the paparazzi surrounding PHP, Perl, Python and Ruby, in my opinion, I see them as lacking a 'major' attribute that I cannot live without. This attribute is extensibility; using existing codebases and libraries written in C and/or Java.

The largest code bases and libraries in the world are either written in C or Java. What use do I get if I cant utilize those libraries? Some might argue that it is possible to call C API from PHP or the others, but have you ever tried writing a PHP extension that wraps a C library? have you tried that in Python? Perl? Ruby?

The amount of learning curve, buggy constructs, and lanuguage intracies you have to deal with make your life suck when it comes to extensibility from those PeePee's (most of them start with the letter P).

Every piece of new hardware, every new database, every robot, each and every mobile device, is guaranteed to provide you a C and/or Java API to communicate with. None have ever provided you with a PHP/Python/Ruby/Perl API. All those API's come as wrapped up contributions of the C or Java version.

This problem does not exist in either C or Java. Java uses the JNI (java native interface) to talk with the C language APIs which could add a learning curve; however, unlike other learning curves, this learning curve aint wasted simply because those two languages are guaranteed to survive for at least the upcoming decade with continuous SDKs sourced out from new technologies.

The PeePee's are useful tools, useful languages, but dont base heavy code bases on them. One day you'll discover that you'll need to start porting your code, let this day be early enough with limited losses.

Wednesday, January 31, 2007

Java Collections, Maps, Sets, Hashs, Lists, Arrays

One thing I really hate is having to use collection types in my java code. Whenever this takes place I have to jump back to the JDK documentation and start digging for methods.

Suddenly, I decided to put an end to this fuss (sorry 3akilmit fuss). I searched google for a simple tutorial, and guess what I found. A wonderfully illustrated simple to use up-to-date small tutorial PDF document, check it out here.

Wonderful and easily digestible by an average mind.