Hot questions for Using Ubuntu in command line

Top Java Programmings / Ubuntu / command line


System details:

Ubuntu 17.10
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.17.10.2-b12)

I can't get my java program to run. I don't know why it won't find the class. It compiles with the -classpath flag, but doesn't find the class when running.

$ ls -ltra
total 668
-rw-rw-r-- 1 bvpx bvpx 653275 Jan 19 14:45 javax.mail.jar
drwxr-xr-x 3 bvpx bvpx   4096 Jan 19 14:59 ..
-rw-r--r-- 1 bvpx bvpx    960 Jan 19 15:07
drwxr-xr-x 2 bvpx bvpx   4096 Jan 19 15:07 .

Compiling without -classpath does not work (I thought -classpath defaulted to .?)

$ javac error: package javax.mail does not exist

Specifying the -classpath helps, the program now compiles and produces Example.class:

$ javac -classpath javax.mail.jar

Here's the source code:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class Example {
    static final int PORT = 587;
    /* ... */

    public static void main(String[] args) throws Exception {
        /* ... */
        Transport transport = session.getTransport();
            transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
            transport.sendMessage(msg, msg.getAllRecipients());
            System.out.println("Email sent!");
        catch (Exception ex) {
            System.out.println("Error message: " + ex.getMessage());

Running the program produces this error:

$ java -Xdiag -classpath javax.mail.jar Example 
Error: Could not find or load main class Example
java.lang.ClassNotFoundException: Example
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    at sun.launcher.LauncherHelper.checkAndLoadMain(

Running java without -classpath causes the JNI to not find javax/mail even though it's in the directory.

$ java -Xdiag Example 
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Address
    at java.lang.Class.getDeclaredMethods0(Native Method)

Why can't java find the Example class?


I had to set -classpath to include the current directory. According to the documentation classpath is delimited by :. The correct classpath string was:


Below is a working example.

$ javac -classpath javax.mail.jar:.  
$ java -classpath javax.mail.jar:. Example 
Email sent!

Another thing to note was that there was originally a package definition at the top of I had to remove it.


I have a java source file named that starts with import Jama.*; and it contains the main method. The current directory contains java source file and the Jama-1.0.3.jar file

I can compile the source using

javac -classpath Jama-1.0.3.jar

This generates a File_2.class file in the current directory which is not getting executed. I have tried the following

$ java File_2
Exception in thread "main" java.lang.NoClassDefFoundError: Jama/Matrix
at File_2.main(


$ java -classpath Jama-1.0.3.jar File_2
Error: Could not find or load main class File_2

Thus my question is how do I execute the File_2.class file ? Kindly help.


You have to add your current directory to in order to find your class file. You must run java -classpath Jama-1.0.3.jar:. File_2.


I have a Grails application, which has a cron job that should start a command line process.

I know that with Gradle I can easily start a command line process, but I do not know how I can do that with Grails.

How can I start a command line process from my Grails/Groovy/Java application?


just be groovy

["touch", "/var/tmp/kilroy_was_here"].execute()


I'm developing, compiling, and running my Java project from the Ubuntu command line. The IDE I was using gave me helpers that generated basic project files for me.

Is there something similar available for the command line?


You can achieve that using Maven, but you have to define simple pom.xml first. Here you can find details:

@EDIT: Here is better tutorial:


I am new to Java and I want to run a simple test-program on an Ubuntu server via the command line. "Hello World" worked well. But now I want to add jar-files to my test program. So I followed this simple tutorial.

I created a file "" like this:

import org.apache.commons.lang3.*;

public class MyTest
   public static void main (String[] args)
            // Print Hello World!
            String x = "Hello World!";

Compiling the java-file with

 javac -cp jars/commons-lang3-3.7.jar

worked well without errors and a MyTest.class() file is generated. But when I want to run the test-program with

java -cp jars/commons-lang3-3.7.jar MyTest

I end up with the following Error:

Error: Main class MyTest could not be found or loaded

What am I missing? Why is the compiler successful but yet the program unable to be executed?


To make it clear: I do NOT want to build a jar file (at least not yet). I just want to build and run a simple java program referencing a jar-file i downloaded from the internet. Like described here:

Java Version:

> java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)

JAVAC Version:

> javac -version
javac 1.8.0_151


You forgot to specify "MainClass" to the

That should be in the last step of Eclipse Jar Export Wizard.

Tell me if you're on another IDE or using Maven, that changes the procedure.

Your current command should run successfully.