Tag Archives: C#

Post navigation

Source Code and Binaries Organization

Source Code

Every source code file must contain maximally one public class. There may be more than one non-public classes.

The name of the public class – if there is any – must be identical to the first part of the source code file.

Source code file: MyLove.java -> public class MyLove

Partial classes are not allowed.

Binaries

From MyLove.java a MyLove.class is created. The .class file contains the Java bytecode of the associated .java file.

From .class files and ressources a XYZ.jar (Java ARchive) can be created. A jar file is mainly a zip file of its contents, maybe with an added metadata file and the file extension .jar instead of .zip.

In principle, a jar file is something similar to an assembly or dll in .NET. The term jar hell is well known to Java developers.

A jar file can be a runnable jar file or a library.

The jar commandline program can be used to create or inspect jar files. It is a zip program with tar like syntax.

jar tvf XYZ.jar shows a verbose content listing of XYZ.jar.

In Eclipse, use File - Export - Java to create a jar file.

Runnable Jar Files

A runnable jar file is a an ordinary jar file with an added manifest file which contains an entry for the main class.

The main class must have a public static void main(String[] args) method. This method will be called when the jar is run.

The manifest file is an ordinary UTF-8 encoded text file called MANIFEST.MF located in the folder META-INF. It can contain information about signing, version control, package sealing and others.

An entry for a main class may look like this

Main-Class: Gaming.TraceOfDeathEngine

Here, Gaming is the package name and TraceOfDeathEngine is the class name.

Create a Runnable Jar, V1

This method will pack all the files of the project into the jar file. There is no way here to select which files shall be included in the jar and which shall not. But as a jar file is an ordinary zip, you can remove superfluous files afterwards. Or see method 2.

On the the following page, select the Java- and resource files you want to have in the jar. Also set the export destination.

On the fourth page, select Generate the manifest file at the top and at the bottom select your Main class.

Create a Runnable Jar, V3

If you don’t use Eclipse or want to automate creating a runnable jar file, you can use the jar utility.
An example:

jar -cvfe troz.jar Gaming.TrodEngine Gaming

The command line above explained:

c: create new jar file
v: be verbose
f: specify archive file name
e: pass entry point, create manifest automatically
troz.jar: the archive file name
Gaming.TrodEngine: the entry point is class `TrodEngine`
in package `Gaming`
Gaming: the folder to include in the jar.

If you have already a created manifest you can pass it to the jar utility with the m option. Jar will then add it to the jar file as META-INF/MANIFEST.MF

jar cvfm trox.jar mf.txt Gaming

As said, the jar utility program has tar like syntax. So the line above means:

c: create new jar file
v: be verbose
f: specify archive file name
m: include manifest file
trox.jar: the archive file name
mf.txt: the manifest file name
Gaming: the folder to include in the jar.

Some quirks of the jar utility:

The archive file and the manifest file (or the entrys point) must be specified in the same order as the f , m and e flags.

If any “file” is a directory then it is processed recursively.

It is not allowed to split the options up. tar cvf trox.jar m MAN Gaming is not possible.

Start a Java Program On Windows

On Windows, when Java is installed properly, you can doubleclick a runnable jar file from Windows Explorer to start it. Or you can create a shortcut and place it onto the desktop and use the shortcut to start the java program. You cannot attach such a shortcut to the taskbar, though.

To start a runnable abc.jar via commandline and pass parameters param1 and param2 to the main classes main method, just call

java.exe -jar abc.jar param1 param2

You can also start a Java program if you don’t have a jar file, but only the needed .class files. To do this Java.exe must be called from the bin directory one level above the package name. When the class to start is called MyLove and resides in the package Xcv and the parameters param1 and param2 shall be passed, the command line shall look like this:

In job advertisements for Java developers, the following acronyms and technologies are mentioned often.
For a C# developer, all of these are unknown at first. So what do they stand for?
I am providing short explanations, often copied from Wikipedia or Stackoverflow. Copied explanations are marked as quotations. The number of mentionings of a technology in job openings is in parentheses added to the end of the acronym, if it is bigger than one. Of course, I did no representative research. I just counted the words I noticed in some of the Java job ads I scanned.

ActiveMQ

Apache ActiveMQ is an open source message broker written in Java together with a full Java Message Service (JMS) client. It provides “Enterprise Features” which in this case means fostering the communication from more than one client or server. Supported clients include Java via JMS 1.1 as well as several other “cross language” clients.

Ajax (2)

Ajax is short for Asynchronous JavaScript and XML.

It is a group of interrelated Web development techniques used on the client-side to create asynchronous Web applications. With Ajax, Web applications can send data to, and retrieve data from, a server asynchronously without interfering with the display and behavior of the existing page.

Ant (2)

Apache Ant is a software tool for automating software build processes. It is similar to Make but is implemented using the Java language, requires the Java platform, and is best suited to building Java projects.

Eclipse (2)

Eclipse is an open source multi-platform IDE written in Java. Its size and ambition are similar to Microsoft’s VisualStudio. Where VisualStudio is the standard to develop for .NET, Eclipse is the standard IDE to develop in Java.

EJB

The EJB specification is one of several Java APIs in the Java EE specification. EJB is a server-side model that encapsulates the business logic of an application. …

The EJB specification intends to provide a standard way to implement the back-end ‘business’ code typically found in enterprise applications (as opposed to ‘front-end’ interface code). Such code addresses the same types of problems, and solutions to these problems are often repeatedly re-implemented by programmers. Enterprise JavaBeans are intended to handle such common concerns as persistence, transactional integrity, and security in a standard way, leaving programmers free to concentrate on the particular problem at hand.

Not to be confused with JavaBeans.

ExtJS

Ext JS is a pure JavaScript application framework for building interactive web applications[1] using techniques such as Ajax, DHTML and DOM scripting.

Ext JS includes a set of GUI-based form controls (or “widgets”) for use within web applications:

text field and textarea input controls

date fields with a pop-up date-picker

numeric fields

list box and combo boxes

radio and checkbox controls

html editor control

grid control (with both read-only and edit modes, sortable data, lockable and draggable columns, and a variety of other features)

tree control

tab panels

toolbars

desktop application-style menus

region panels to allow a form to be divided into multiple sub-sections

GWT

Google Web Toolkit, or GWT Web Toolkit, is an open source set of tools that allows web developers to create and maintain complex JavaScript front-end applications in Java. Other than a few native libraries, everything is Java source that can be built on any supported platform with the included GWT Ant build files. It is licensed under the Apache License version 2.0.

Jasper Reports

JasperReports is an open source Java reporting tool that can write to a variety of targets, such as: screen, a printer, into PDF, HTML, Microsoft Excel, RTF, ODT, Comma-separated values or XML files.

It can be used in Java-enabled applications, including Java EE or web applications, to generate dynamic content.

JavaBeans

JavaBeans are reusable software components for Java. They are classes that encapsulate many objects into a single object (the bean). They are serializable, have a 0-argument constructor, and allow access to properties using getter and setter methods.

Not to be confused with Enterprise JavaBeans.

Java EE, J2EE (5)

J2EE is actually a collection of technologies and APIs for the Java platform designed to support “Enterprise” Applications which can generally be classed as large-scale, distributed, transactional and highly-available applications designed to support mission-critical business requirements.

In terms of what an employee is looking for in specific techs, it is quite hard to say, because the playing field has kept changing over he last 5 years. It really is about the class of problems that are being solved more than anything else. Transactions and distribution are key.

It is important to understand that Java EE is a set of specifications and there do exist several implementations.

The specifications (defined by Sun) describe services, application programming interfaces (APIs), and protocols. In general, enterprise applications refer to software hosted on servers that provide the applications that support the enterprise.

The 13 core technologies that make up J2EE are:

JDBC

JNDI

EJBs

RMI

JSP

Java servlets

XML

JMS

Java IDL

JTS

JTA

JavaMail

JAF

JBoss (2)

WildFly, formerly known as JBoss AS, or simply JBoss, is an application server authored by JBoss, now developed by Red Hat. WildFly is written in Java, and implements the Java Platform, Enterprise Edition (Java EE) specification. It runs on multiple platforms.

And about Java application servers:

In the case of Java application servers, the server behaves like an extended virtual machine for running applications, transparently handling connections to the database on one side, and, often, connections to the Web client on the other.

JDBC (2)

JDBC means Java DataBase Connectivity.

JDBC is a Java-based data access technology (Java SE platform). This technology is an API for the Java programming language that defines how a client may access a database. It provides methods for querying and updating data in a database. JDBC is oriented towards relational databases.

Some examples using JDBC and a MySql database are given in chapter…

Jenkins

Jenkins is an open source continuous integration tool written in Java. The project was forked from Hudson after a dispute with Oracle.

JSF, JSFUnit (2)

JavaServer Faces (JSF) is a Java specification for building component-based user interfaces for web applications. It … is part of the Java Platform, Enterprise Edition.

JSFUnit is a test framework for JSF applications. It is designed to allow complete integration testing and unit testing of JSF applications using a simplified API.

JSP

JavaServer Pages (JSP) is a technology that helps software developers create dynamically generated web pages based on HTML, XML, or other document types. Released in 1999 by Sun Microsystems,[1] JSP is similar to PHP, but it uses the Java programming language.

JUnit (4)

JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks which is collectively known as xUnit

Maven (3)

Maven is a build automation tool used primarily for Java projects. Maven addresses two aspects of building software: First, it describes how software is built, and second, it describes its dependencies. Contrary to preceding tools like Apache Ant it uses conventions for the build procedure, and only exceptions need to be written down.

Play

Play is an open source web application framework, written in Scala and Java, which follows the model–view–controller (MVC) architectural pattern. It aims to optimize developer productivity by using convention over configuration, hot code reloading and display of errors in the browser.

RESTEasy

RESTEasy is a JBoss project that provides various frameworks to help you build RESTful Web Services and RESTful Java applications.

Spring (7)

The Spring Framework is an open source application framework and inversion of control container for the Java platform. The framework’s core features can be used by any Java application, but there are extensions for building web applications on top of the Java EE platform. Although the framework does not impose any specific programming model, it has become popular in the Java community as an alternative to, replacement for, or even addition to the Enterprise JavaBean (EJB) model.

Convention over configuration: a rapid application development solution for Spring-based enterprise applications is offered in the Spring Roo module

Authentication and authorization: configurable security processes that support a range of standards, protocols, tools and practices via the Spring Security sub-project (formerly Acegi Security System for Spring).
Remote management: configurative exposure and management of Java objects for local or remote configuration via JMX

Swing (2)

Swing is the primary Java GUI widget toolkit. It is part of Oracle’s Java Foundation Classes (JFC) — an API for providing a graphical user interface (GUI) for Java programs.

Swing was developed to provide a more sophisticated set of GUI components than the earlier Abstract Window Toolkit (AWT). Swing provides a native look and feel that emulates the look and feel of several platforms, and also supports a pluggable look and feel that allows applications to have a look and feel unrelated to the underlying platform. It has more powerful and flexible components than AWT. In addition to familiar components such as buttons, check boxes and labels, Swing provides several advanced components such as tabbed panel, scroll panes, trees, tables, and lists.

Swing is written in Java and built on top of AWT and Java 2D.

SWT

SWT is short for Standard Widget Toolkit. It is a graphics toolkit competing with Swing and AWT. Like AWT and unlike Swing, SWT uses elements and widgets which are native on the underlying OS.

Tomcat (2)

Apache Tomcat (or simply Tomcat, formerly also Jakarta Tomcat) is an open source web server and servlet container developed by the Apache Software Foundation (ASF). Tomcat implements the Java Servlet and the JavaServer Pages (JSP) specifications from Sun Microsystems, and provides a “pure Java” HTTP web server environment for Java code to run in.

Wicket

Apache Wicket, commonly referred to as Wicket, is a lightweight component-based web application framework for the Java programming language conceptually similar to JavaServer Faces and Tapestry.

Date, Calendar And Joda-Time

Very early developers found out that Java’s Date class is bad and it is not possible to improve it. Because of this, most methods of Java’s Date class are deprecated now and early the class Calendar has been added.

But soon after adding Calendar, people realized that Calendar is not very good as well. Then development of the successor API Joda-Time started.

Joda-Time is available from Java SE 8 on. It is located in the namespace java.time and contains the main classes LocalDate, LocalTime and LocalDateTime. Joda-Time contains full support of timezones with classes ZoneId, ZonedDateTime, OffsetTime, OffsetDateTime and ZoneOffset. The older class java.util.TimeZone is deprecated.

Joda-Time supports periods of time with the classes Period and Duration. Both are similar to C#’s TimeSpan. Period and Duration deal with different precisions.

// 3 years, 2 months, 1 day
Period period = Period.of(3, 2, 1);
// You can modify the values of dates using periods
LocalDate newDate = oldDate.plus(period);
// Components of a Period are represented by ChronoUnit values
assertEquals(1, period.get(ChronoUnit.DAYS));
// A duration of 3 seconds and 5 nanoseconds
Duration duration = Duration.ofSeconds(3, 5);

As expected, you can add and subtract durations and you can modify date values with durations.

Joda-Time also contains

Chronolgy and associated classes to represent calendars different from the Gregorian calendar.

MonthDay to represent a birthday.

YearMonth to represent a credit card expiration date.

From Java SE 8 on all three date-time systems (Date, Calendar and Joda-Time) exist in Java in parallel.

Checked Exceptions

In Java, there are two types of exceptions. Checked and unchecked exceptions. Unchecked exceptions are those which derive from RuntimeException, checked exceptions extend Exception. In C#, there are only unchecked exceptions.

Checked exceptions are an interesting feature.

If a method X can throw one or several checked exceptions, it must announce this in its declaration with the throws keyword like this:void ReadMyFile(String filename) throws IOException

What follows: if a method M calls a method X which may throw a checked exception Ex,

M itself must catch Exor

M itself declares that it may throw Ex via throws Ex

This is enforced by the compiler.
At first glance, it seems that checked exceptions make things easier for library developers and library users… But at second glance, it seems checked exceptions are deprecated in the Java community. Probably the main point of this interesting post about checked exceptions is:

When you choose to use checked exception for errors that you think are recoverable, you try to decide for the client of your API, whereas only he may know what’s recoverable and what isn’t in his own context.