Hot questions for Using Ubuntu in command line

Top Java Programmings / Ubuntu / command line

Question:

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 Example.java
drwxr-xr-x 2 bvpx bvpx   4096 Jan 19 15:07 .

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

$ javac Example.java 
Example.java:2: error: package javax.mail does not exist

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

$ javac -classpath javax.mail.jar Example.java
$ 

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();
        try
        {
            System.out.println("Sending...");
            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.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)

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?


Answer:

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

javax.mail.jar:.

Below is a working example.

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

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

Question:

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

I can compile the source File_2.java using

javac -classpath Jama-1.0.3.jar File_2.java

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(File_2.java:32)

and

$ 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.


Answer:

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.

Question:

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?


Answer:

just be groovy http://groovy-lang.org/groovy-dev-kit.html#process-management:

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

Question:

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?


Answer:

You can achieve that using Maven, but you have to define simple pom.xml first. Here you can find details: http://maven.apache.org/guides/mini/guide-creating-archetypes.html

@EDIT: Here is better tutorial: http://www.mkyong.com/maven/how-to-create-a-java-project-with-maven/

Question:

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 "MyTest.java" like this:

import org.apache.commons.lang3.*;

public class MyTest
{
   public static void main (String[] args)
   {
            // Print Hello World!
            String x = "Hello World!";
            System.out.println(StringUtils.capitalize(x));
   }
}

Compiling the java-file with

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

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?

EDIT:

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: https://www.programcreek.com/2014/01/compile-and-run-java-in-command-line-with-external-jars/

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

Answer:

You forgot to specify "MainClass" to the Manifest.mf

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.