For quick development work, I’m not interested in using netbeans or eclipse or some other IDE, but the majority of java developers use IDEs. This is why, when that inevitable java ‘package com.xx does not exist’ error occurs, a lot of people who use IDEs can’t or won’t tell how to solve it, e.g. https://forums.oracle.com/forums/thread.jspa?threadID=1756037 or answers like “Honestly, you need to read a Beginning Java book.”.
If java isn’t part of your career or fun, and you have better things to do than reading a book, here is how to work with Java, to write and execute a program within 5 minutes (but spend a minute reading the whole text here first).. (disclaimer: I’m no expert in java).
First off, install java (as root user type rpm -ivh jdk-7u2-linux-x64.rpm for 64-bit linux). It will install in /usr/bin.
Create a folder for your work, e.g. if your home directory is called myname, then ~myname/development/project1 might be good.
Usually, you’ll be including some libraries (e.g. say a PDF library if you’re creating PDFs), so they need to be part of the classpath (either an environment variable or part of the command line of the javac compiler; we’ll use the latter method below). If you have downloaded some jar files to the project1 folder, then they need to be extracted, using the jar xf command,
jar xf itextpdf-5.1.3.jar
If you have a look, it will have created a new folder (usually com but can be something else).
In fact, it is a chain of sub-folders, e.g. com/itextpdf/text in your project1 folder.
Now you can create your java source code in the project1 folder – name the file with a capital (uppercase) letter, this is a good convention. For example, Demo.java.
The capital allows you to know that this is also the name of the class.
The file should contain something like this:
public class Demo
public static void main(String args)
The ‘package’ line contains a com.. line that you can make up, but then make sure that you use mkdir to create that path too (the -p tells mkdir to construct all folders in that path if they don’t exist):
mkdir -p com/blah/myproject
The ‘import’ lines are used to include functionality that you want to use from other libraries. In the example above, the first two are part of the java installation, and the next two are part of the downloaded jar file (and the jar command created that path too). Notice that the capital letter in the ‘import’ lines is denoting a class name.
Notice that the class name ‘Demo’ in this file is the same name as the filename Demo.java, this is important.
Save the file, and then compile. For example:
/usr/bin/javac -deprecation -classpath “.:/usr/java/default/lib/tools.jar” -source 1.6 -target 1.6 -sourcepath “./.” Demo.java
(ignore any warning unless it seems important).
Once compiled, there will be a Demo.class file. Copy or move it into the com/blah/myproject folder.
Now you can execute the java program, using:
Note that for any substantial development, Netbeans or Eclipse or some other IDE should be used. Most java programmers don’t tend to use ‘makefiles’, the java world equivalent is ‘ant’. However I prefer makefiles for the very small java work I’m interested in. You may wish to use a makefile, one is below (save the Makefile.txt file as Makefile). After you have executed any jar command, you can just type ‘make clean’ and ‘make all’ or ‘make Demo.class’.