From Wikitech
Jump to navigation Jump to search


This page contains information about developing tools in Java.

Java frameworks

You can choose from a number of Java frameworks to work with. A list can be found here:

jar files

Your completed Java project should be built to result in a jar file. You can upload the jar file to your tool account in Toolforge, along with any other libraries dependencies.

Execute your jar file with command line java

For example, if your jar file name is fixLink.jar, then you can execute it by following these 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 run.

Follow this method each time you need to manually run the jar file.

Schedule tasks

When you need to schedule the Java tool you should use crontab.

Crontab is a file you can append some commands into that lets you schedule your tools for any interval of time, (every 5 min, 1 hour, 24 hours...etc).

We recommend that you use the jsub command when writing cron for your java tool.

Schedule a java tool:

Following is an example for scheduling your Java tool with crontab:

  • Choose the folder in your tool you want to run.
    • For example, you have a folder in your tool called test, and it contains your jar file (test.jar).
  • Type crontab -e.
  • Choose the interval of time you want to use and write the Cron syntax.
    • For example, if you want the tool to run every 5 minutes 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 your cron job is now scheduled.

Note: Java consumes a lot of RAM memory, and it's very sensitive about paths. 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.

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://tools.db.svc.eqiad.wmflabs:3306/";
    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: Error occurred during initialization of VM, Could not allocate metaspace, Could not reserve enough space for code cache: make sure you used this phrase when creating your cron job: -mem 9000m. It allocates memory to the JVM, so it can be run successfully without any errors.
  • Error: Unable to access jarfile: This error happens when you don'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. Java consumes a lot of memory, and it needs large memory space to run jar files. So, refer to the previous example:
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.

Communication and support

We communicate and provide support through several primary channels. Please reach out with questions and to join the conversation.

Communicate with us
Connect Best for
Phabricator Workboard #Cloud-Services Task tracking and bug reporting
IRC Channel #wikimedia-cloud connect
Telegram bridge
mattermost bridge
General discussion and support
Mailing List cloud@ Information about ongoing initiatives, general discussion and support
Announcement emails cloud-announce@ Information about critical changes (all messages mirrored to cloud@)
News wiki page News Information about major near-term plans
Cloud Services Blog Clouds & Unicorns Learning more details about some of our work
Wikimedia Technical Blog News and stories from the Wikimedia technical movement

See Also