From Wikitech
Jump to navigation Jump to search

If you use java in writing tools, then you should build your java project to be jar file. Then you can upload the jar file to your tool account in Toolforge, along with any other libraries dependencies. You can run your java tool with command line java. For example, if your jar file name is "fixLink.jar", then you can execute it by following the steps:

    • Change directory to the folder that jar file exist in. e.g. cd fixLink/dist
    • Prompt the following command line: java -jar fixlink.jar
    • Type enter, and the program should be worked.

Remember that the previous way is to run java tool manually, that mean each time you need to run the jar file, you should follow the previous steps.

MediaWiki API java frameworks

There are many MediaWiki API java frameworks, you can find the list of the frameworks here. You can use any one of them, but remember, after complete coding your java project, you have to build it, so it will result in a jar file.

Schedule tasks

When you need to schedule the java tool, to be executed daily for example, you should use crontab. Crontab is simply a file, that you can append some commands in to, and it let you to schedule your tools, every amount of time. (every 5min, hour, day, 12a.m. each day...etc). Java consumes a lot of RAM memory, and it's very sensitive about paths, so, you may face some common errors when you try to write cron for your tools. See the section of errors if you face any of them.

We recommend to use jsub command when writing cron for your java tool. The proper way to schedule a java tool is the following:

  • As an example, you have a folder in your tool, which called test, and it contains your jar file (test.jar).
  • Type cron -e.
  • Write your cron syntax you want, for example, we want to run the tool each five minutes, so we write: */5 * * * *.
  • After that, append your tool execution command, so you write: jsub -mem 9000m -once /usr/bin/java -jar /data/project/YourToolName/test/dist/test.jar
  • Your final cron code is */5 * * * * jsub -mem 9000m -once /usr/bin/java -jar /data/project/YourToolName/test/dist/test.jar
  • Save the file, and the cron command for your java tool should be worked now.

You can change the previous steps depending on your needs.

Note: This phrase is very important: -mem 9000m. It allocates memory to the JVM, so it can be run successfully without any errors. Otherwise, you will face errors like Error occurred during initialization of VM, Could not allocate metaspace, Could not reserve enough space for code cache.

Connecting from a Servlet in Tomcat

  1. create directory "lib" in directory "public_tomcat"
  2. copy "mysql-connector-java-bin.jar" to "public_tomcat/lib"
  3. import org.apache.tomcat.jdbc.pool.DataSource;
    import org.apache.tomcat.jdbc.pool.PoolProperties;
    String DBURL = "jdbc:mysql://";
    String DBDRIVER = "com.mysql.jdbc.Driver";
    String DATABASE = DBUSER + "__" + PROJECT;
    PoolProperties p = new PoolProperties();
    p.setUrl (DBURL + DATABASE);
    p.setDriverClassName(DBDRIVER );
    p.setUsername (DBUSER );
    p.setPassword (DBPASSWORD );
    	"org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;" +
    DataSource datasource = new DataSource();
    Connection connection = datasource.getConnection ();
    Statement statement = connection.createStatement();
  4. javac -classpath javax.servlet.jar:tomcat-jdbc.jar

Common errors

Java versions

The common error may face you when running your java tool in Toolforge is the difference between java versions. The java version installed in Toolforge is 1.7 (also known as java 7). If you write your code in higher versions of java; you'd have to change the version of your project; so it can be equal or lower than Toolforge java version. If you use Netbeans IDE, you can change the java version of the project by:

  • Right click on project, and select proporties.
  • In sources category; change source/Binary Format to JDK 7.
  • In libraries category; change java platform to JDK 1.7.
  • Click Ok.

Crontab errors

There are some errors that may face you when trying to write crons:

  • Error: Unable to access jarfile: This error happens often when you didn't point to the exact path of the jar file. Cron works only if you give it the absolute path. For example, you have a java tool folder in you tool account, which named is "test", inside test folder there is a dist folder; which contains the jar file, and the lib folder, which contains any dependent libraries. Now, if you write cron like this:
java -jar /test/dist/test.jar
You need to rewrite the cron with absolute paths, like:
usr/bin/java -jar /data/projects/YourToolName/test/dist/test.jar
  • Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit.: This error means that java needs more memory to run your tool. As we said above, java consumes a lot of memory, and it needs large memory space to run jar files. So, refer to the previous example, if you write like this:
usr/bin/java -jar /data/projects/YourToolName/test/dist/test.jar
You have to add -mem command to the cron line:
jsub -mem 9000m -once java -jar /data/project/YourToolName/test/dist/test.jar

  • Error occurred during initialization of VM Could not reserve enough space for object heap Error occurred during initialization of VM Could not reserve enough space for object heap: This error tells you that the value of memory you have specified is too low for java. You can fix this error by raising the value.