You are not logged in.

#1 2014-09-24 13:58:41

Marcel-
Member
From: Utrecht, NL
Registered: 2006-12-03
Posts: 266

[SOLVED] Java could not find or load main class

I just tried to run a Java program using Swing components from the terminal, but I always get this error message:

$ java VisualTest
Error: Could not find or load main class VisualTest

The strange thing is that it runs flawlessly from within Eclipse.

I created VisualTest.java using the (old) Visual Editor, it's contents are:

package visual1;

import javax.swing.SwingUtilities;
//import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class VisualTest extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				VisualTest thisClass = new VisualTest();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public VisualTest() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setContentPane(getJContentPane());
		this.setTitle("JFrame");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
		}
		return jContentPane;
	}

}

As you can see, there is a class named VisualTest and it contains a main function.

My CLASSPATH variable contains a dot, like

.:/home/mk/.java/deployment/ext/*

I just updated my Java from JDK 7u67 to JDK 8u20 (the Oracle one, in AUR), but I can't imagine this is related (however, I haven't run Java programs from the terminal for a long time).

Strangely enough, a simple HelloWorld.java

public class HelloWorld {
	public static void main(String[] args) {
		System.out.println("Hello World!");
	}
}

runs flawlessly from within the terminal.

Last edited by Marcel- (2014-09-26 12:44:35)

Offline

#2 2014-09-24 15:07:25

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,803

Re: [SOLVED] Java could not find or load main class

What directory are you in?  Eclipse does not store the compiled stuff in the same directory as the source.   When you compile by hand from the cli, by default, the compiled output gets placed in the same directory.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2014-09-24 16:50:59

Marcel-
Member
From: Utrecht, NL
Registered: 2006-12-03
Posts: 266

Re: [SOLVED] Java could not find or load main class

As intended by my institution, I said to Eclipse that it should store sources and class files in the same directory. Let's do ls and test:

[~/workspace]$ ls
total 12K
drwxr-xr-x 4 mk mk 4.0K Sep 23 18:23 visualtest/
-rw-r--r-- 1 mk mk  426 Sep 24 15:45 HelloWorld.class
-rw-rw-r-- 1 mk mk  111 Sep 24 15:44 HelloWorld.java
[~/workspace]$ java HelloWorld 
Hello World!

vs.

[~/workspace/visualtest/visual1]$ ls
total 12K
-rw-r--r-- 1 mk mk  528 Sep 24 15:38 VisualTest$1.class
-rw-r--r-- 1 mk mk 1.1K Sep 24 15:38 VisualTest.class
-rw-r--r-- 1 mk mk 1.2K Sep 24 00:32 VisualTest.java
[~/workspace/visualtest/visual1]$ java VisualTest 
Error: Could not find or load main class VisualTest

I also tested this with another project that runs fine in Eclipse, but it gives the same error message when trying to issue `java StemOpPartijFrame`.

EDIT:

One difference between the two OSs though is that Linux progresses.  The other decays..

Two? I count many! And I don't think something like OpenBSD decays. ;-)

Last edited by Marcel- (2014-09-24 16:54:12)

Offline

#4 2014-09-26 12:19:13

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [SOLVED] Java could not find or load main class

I'm going from memory here.

It's the package that is screwing things up. The class is not just "VisualTest", which would imply that it is not part of any package; it's actually "visual1.VisualTest". But when you specify the package on the command line

$ java visual1.VisualTest

it will look for a file "visual1/VisualTest.class", so to make it succeed, be in this directory:

$ cd ~/workspace/visualtest

Offline

#5 2014-09-26 12:45:03

Marcel-
Member
From: Utrecht, NL
Registered: 2006-12-03
Posts: 266

Re: [SOLVED] Java could not find or load main class

Indeed, that's it! Thanks!

Offline

Board footer

Powered by FluxBB