Roman
Roman

Reputation: 131258

How to compile a .java file in Java?

I have the following code generated by Eclipse (.java file).

import org.eclipse.swt.widgets.Shell;

import org.eclipse.swt.widgets.Display;

public class HelloWorldSWT {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Hello world!");
        shell.open();
        while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }

}

Now I want to compile the above file from the command line. I went to the directory where the source code is located and I tried two commands:
1. javac HelloWorldSWT.java
2. javac -d /home/myname/workspace/ HelloWorldSWT.java

In both cases I have the same error "The import org.eclipse cannot be resolved". /home/myname/workspace/ - is the directory where the class file is located.

As far as I understand the compiler does not see the org.eclipse.swt package. Why?

Can it be because the problematic package is located in "/home/myname/workspace/org.eclipse.swt/" (not in "/home/myname/workspace/org/eclipse/swt/")?

Upvotes: 7

Views: 90212

Answers (7)

St.Shadow
St.Shadow

Reputation: 1842

Ok, Stephen C I did this job by hand. I used only Notepad++ (I promise)

  1. Start Notepad++ and create file HelloWorldSWT.java
  2. Copy example from author
  3. Save it!
  4. Open cmd and go to the directory with HelloWorldSWT.java
  5. Run the command javac HelloWorldSWT.java

  6. Ok, go to the Eclipse directory and find the correct jar swt-3.4.2-win32-win32-x86.jar

  7. Run this again

    D:\workspaces\spf_workspace\hand-made>javac -cp "D:\Program files\eclipse3_5\plugins\org.eclipse.swt.win32.win32.x86_3.5.1.v3555a.jar" HelloWorldSWT.java

All process take 2 minutes.

Don't try to run this:

`D:\workspaces\spf_workspace\hand-made>java -cp "D:\Program files\eclipse3_5\plugins\org.eclipse.swt.win32.win32.x86_3.5.1.v3555a.jar;." HelloWorldSWT`

Note: I add current dir . to classpath too.

Upvotes: 5

Stephen C
Stephen C

Reputation: 719446

@Roman - this problem is too complicated for a beginner to try to address. The problem is that SWT has complicated dependencies, including dependencies on native code libraries.

You are best off running your SWT application using Eclipse "RunAs" ... or trying to find some Eclipse-specific documentation on running SWT-based applications from the command line.

Upvotes: 0

James B
James B

Reputation: 3750

The classpath variable or command line switch needs to point to where the org.eclipse.swt.widgets.Shell class resides, if this class is inside a jar file, then the classpath needs to contain the actual jar file,

i.e. javac -classpath /root/to/jar/eclipse.jar

Otherwise, if the org.eclipse.swt.widgets.Shell class is just a loose class file (which I doubt, I assume it will be inside one of the eclipse jar files, which you can list using jar -tvf jar-you-think-it-might-be-in.jar)...then you will need the javac -classpath to point to the location of the top level directory within the org/eclipse/swt/widgets/ path.

Upvotes: 0

deleted
deleted

Reputation: 195

But I though that I specify the "classpath" during the compilation (using -d option). I though that after the "-d" option I put the name of directory where all my packages are located. Do I understand that wrongly?

try

javac -help

to see what the different command line options do. also note the other post above that explains this.

compiling from the command line and setting up classpath and everything right is a pain. however, it is useful to do it so that you understand what the ide actually does when it automates this for you.

Upvotes: 0

Thilo
Thilo

Reputation: 262814

Since you are doing Eclipse RCP development, you should let Eclipse handle your compilation as well. (You will most likely find your classes in a "build" or "bin" directory in the project). In addition to compilation, there will be some "packaging" steps to create the final application, and Eclipse has tools for that, too.

If you really want to build outside of Eclipse, you need to manage a potentially large list of dependencies (such as org.eclipse.swt.widgets), which makes a pure javac unfeasible. You would need to look at Ant or Maven.

Also note that you will need the classpath to include dependencies not only for compilation, but also when you run the program.

Upvotes: 2

St.Shadow
St.Shadow

Reputation: 1842

You forgot about classpath

Upvotes: -2

benzado
benzado

Reputation: 84378

You need to set your classpath so that the Java compiler knows where to find the org.eclipse.* classes. You can do that with a command line switch or an environment variable.

Upvotes: 5

Related Questions