You should be able to run it after compiling it

javac TestJDBC.java
java TestJDBC

Note that you do not need to add .class when running it from the commandline.

If this still does not work, please paste your code.

EDIT after request

So you've now stated that you're missing javac from your PATH. I'll show you how to add it:

$> export JAVA_HOME=/path/to/jdk/jdk.1.8.0_20
$> export PATH=$PATH:$JAVA_HOME/bin

Verify by running

javac -version

It should print something like

javac 1.8.0_20
Answer from Robin Jonsson on Stack Overflow
🌐
Oracle
java.com › en › download › help › enable_console_linux.html
How do I enable and view the Java Console for Linux or Solaris?
Open the Java Control Panel. Enter: ./ControlPanel · In the Java Control Panel, click the Advanced tab. Select Show console under the Java Console section.
🌐
Linux Hint
linuxhint.com › run-java-command-line-linux
How to Run Java from Command-line in Linux – Linux Hint
To run the java program in Linux, we need to verify if Java Development Kit (JDK) is available in the system and its version.
🌐
Ixiasoft
ixiasoft.com › documentation › IXIASOFT_CCMS › 5.0 › Administration_Guides › per1389986400062_7.html
Turn on the Java Console
This procedure describes how to turn on the Java Console and enable its debugging and logging options.
🌐
Princeton CS
introcs.cs.princeton.edu › java › 15inout › linux-cmd.html
Java and the Linux Command Line
August 14, 2019 - From the shell, type the java command below. [wayne] ~/introcs/hello> java HelloWorld Hello, World If all goes well, you should see the output of the program - Hello, World. If your program gets stuck in an infinite loop, type Ctrl-c to break out. If you are entering input from the keyboard, you can signify to your program that there is no more data by typing Ctrl-d for EOF (end of file). You should type this character on its own line.
🌐
Linux Mint Forums
forums.linuxmint.com › board index › main edition support › software & applications
How do I open Java Control Panel? - Linux Mint Forums
October 31, 2016 - If updatedb is set to run, which I'm assuming is the default, you should be able to type on the command line locate -i controlpanel and locate -k bin/java. ./NAME says to run the executable (bash script, python script, compiled program) from the current directory (A dot is the current directory ...
Top answer
1 of 14
21

I found this while looking for an answer myself, I ended up writing this bit:

/**
 * This opens a command line and runs some other class in the jar
 * @author Brandon Barajas
 */
import java.io.*;
import java.awt.GraphicsEnvironment;
import java.net.URISyntaxException;
public class Main{
    public static void main (String [] args) throws IOException, InterruptedException, URISyntaxException{
        Console console = System.console();
        if(console == null && !GraphicsEnvironment.isHeadless()){
            String filename = Main.class.getProtectionDomain().getCodeSource().getLocation().toString().substring(6);
            Runtime.getRuntime().exec(new String[]{"cmd","/c","start","cmd","/k","java -jar \"" + filename + "\""});
        }else{
            THEMAINCLASSNAMEGOESHERE.main(new String[0]);
            System.out.println("Program has ended, please type 'exit' to close the console");
        }
    }
}

not sure if my answer is still relevant, but feel free to use it with the comment kept in o/

Only flaw I can think of is that it leaves the cmd window open after the program completes.

Usage: place this class in the same package as your main class and set it as the main class, it will open a command prompt window if one is not open, or if one is open launch the main class. Name / location of jar file is automatic. Designed for windows, but if you want it for another system just message me and I'll fix it. (I could do OS detection but I'm lazy and just making this so I can turn in a double-click jar file to my professor who uses windows).

2 of 14
5

If you want full control, you can implement a Console window in Swing which does what you have now.

If you cannot open said window (if headless) or the user asks for it on the command line, then just default to your current behaviour.

Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 10 › deploy › java-control-panel.htm
9 Java Control Panel
To start the Java Control Panel from the command line, enter <JRE installation home>\bin\javacpl.exe on Windows, or <JRE installation home>/bin/jcontrol on macOS or Linux.
🌐
CodingNomads
codingnomads.com › how-to-run-java-from-command-line
Executing Java applications from the CLI
A Java application can be run and compiled from the command line. Although running Java apps from the CLI during normal development is not entirely common, it is essential to know how it is done. Please follow the steps below to run your first Java app from the CLI. First, open your terminal and ensure you're in the right working directory.
🌐
SolVPS Hosting Blog
solvps.com › blog
Open Java Control Panel Settings from CMD (Windows) | SolVPS Hosting Blog
August 30, 2017 - If the path is not found, navigate manually to c:\Program Files (x86)\Java\ and find the .exe within the version folder you have installed.
🌐
Oracle
java.com › en › download › help › javaconsole.html
How do I enable and view the Java Console?
Select Show Console and click OK. NOTE: These instructions apply if you've chosen to place the Java icon in the system tray through the Java Control Panel (Advanced tab) ... Select Open Console.
🌐
Medium
medium.com › @AlexanderObregon › executing-java-code-from-the-command-line-d6a3c09bb565
Executing Java Code from the Command Line | Medium
March 9, 2025 - Learn how to compile and run Java programs from the command line using javac and java, manage classpaths, and understand how the JVM processes bytecode.
🌐
Stack Abuse
stackabuse.com › executing-shell-commands-with-java
Executing Shell Commands with Java
May 18, 2020 - In this tutorial, we'll cover how to execute shell commands, bat and sh files in Java. We'll be covering examples for all exec() and ProcessBuilder approaches.
🌐
MIT
web.mit.edu › 6.031 › www › fa17 › projects › fb1 › commandline.html
Running Java Programs with Command-Line Arguments
Open a command prompt and cd into your project folder. Then run the java command with these arguments: · java -cp bin:lib/parserlib.jar:lib/physics.jar some.package.Main filename1 filename2 …
🌐
Mkyong
mkyong.com › home › java › how to execute shell command from java
How to execute shell command from Java - Mkyong.com
January 3, 2019 - import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ProcessBuilderExample1 { public static void main(String[] args) { ProcessBuilder processBuilder = new ProcessBuilder(); // Windows processBuilder.command("cmd.exe", "/c", "ping -n 3 google.com"); try { Process process = processBuilder.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } int exitCode = process.waitFor(); System.out.println("\nExited with err