7

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/")?

RAS
  • 8,100
  • 16
  • 64
  • 86
Roman
  • 124,451
  • 167
  • 349
  • 456
  • 1
    I know this is a serious question but i just had to point out what a hilarious sounding title "How to compile a .java file in Java?" is. – James Oswald Oct 01 '16 at 01:41

7 Answers7

5

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.

benzado
  • 82,288
  • 22
  • 110
  • 138
  • 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? – Roman Feb 17 '10 at 09:22
  • 1
    Roman - the /d is where the compiled classes (ie the .class files) go... The classpath (isn't Java fun?) can be set with either the CLASSPATH Environment variable, or using the -classpath option of the javac program. – Martin Milan Feb 17 '10 at 09:32
  • Thank you, Martin. Now it is more clear to me. However, I still get the same error message when I try "javac -d /home/myname/workspace/HelloWorldSWT/ -classpath /home/myname/workspace/ HelloWorldSWT.java". – Roman Feb 17 '10 at 09:45
  • my guess it org.eclipse.* class files are not in your directory but in some eclipse directory – Roman A. Taycher Feb 17 '10 at 09:58
  • They are in the plugins directory in your eclipse installation. But good luck figuring which of the 100 or so JAR files in that directory you actually need! – Stephen C Feb 17 '10 at 10:05
  • Roman, I have /home/myname/workspace/org.eclipse.swt/ directory. There are not class-files in this directory but there are some jar-files. I just was able to compile my code by: javac -d /home/myname/workspace/HelloWorldSWT/ -cp /home/myname/workspace/org.eclipse.swt/swt.jar HelloWorldSWT.java – Roman Feb 17 '10 at 10:07
5

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.

Keanu
  • 53
  • 9
St.Shadow
  • 1,840
  • 1
  • 12
  • 16
  • If you run it like that, how is SWT going to find its native (JNI) library? – Stephen C Feb 17 '10 at 10:57
  • org.eclipse.swt.win32.win32.x86_3.5.1.v3555a.jar contains native dll inside and during running it unzip it to system tmp dir (java use system tmp as placeholder for native libs by default as I know) – St.Shadow Feb 17 '10 at 11:01
2

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.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • But for understanding classpath/dependancy mechanism in java fully - compiling from shell is the best way. And using Notepad++ as IDE -) – St.Shadow Feb 17 '10 at 09:21
  • Many people says that I will find my classes in "build" or "bin". But I found my class-file in the /home/myname/workspace/HelloWorldSWT/. Or we talking about different things? Can the dependencies problem be large in my simple case? I just want javac to see one package (org.eclipse)? – Roman Feb 17 '10 at 09:27
  • For learning classpath/dependency issues, maybe an SWT project is not a good way to start... – Thilo Feb 17 '10 at 09:38
  • 1
    @St Shadow - that is true in theory. In practice, you should not pick an example as messy as Eclipse SWT / RCP for learning about these mechanisms. – Stephen C Feb 17 '10 at 09:45
  • Normal. SWT project is the same as any project where used at least one jar :) – St.Shadow Feb 17 '10 at 09:46
  • @Stephen C, author of question create SWT based project, not RCP. Because creating RCP at notepad is really overcomplicated thing. – St.Shadow Feb 17 '10 at 09:48
  • @St Shadow - the problem is that SWT potentially requires many JARs, not to mention the supporting native libraries. This is NOT a simple task. Even if RCP is not involved. (And if we are quibbling, he didn't create it with NotePad. You are the one who introduced Notepad into the conversation.) – Stephen C Feb 17 '10 at 09:49
  • @Stephen C, *.bat and *.sh were invented for this. My Idea is that working with dependencies (especially with many dependencies) is the best way for learning (at first) and understanding (also very good) the idea of classpath. Because putting on build path at IDE without understandig the full picture is the way to jar-hell. – St.Shadow Feb 17 '10 at 09:56
  • @St Shadow : Ok for learning with a real project, but Eclipse developpement is a MESS of libs. It's like learning how to build a wall by being in charge of a skyscraper construction. – Valentin Rocher Feb 17 '10 at 09:58
  • @St Shadow - if you believe it easy enough for a beginner, why don't you prove it by showing us how it is done? :-) – Stephen C Feb 17 '10 at 10:01
  • @Stephen C, I provide link to official guide from java.sun.com, where details of setting classpath are shown. I can give fishing, not fish. – St.Shadow Feb 17 '10 at 10:13
  • @St Shadow - so are you too chicken to take up my challenge?!? :-) – Stephen C Feb 17 '10 at 10:19
0

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.

deleted
  • 195
  • 2
  • Thanks. I thing I got the idea. -d specify the directory where the class files, generated during the compilation, should go. And -classpath specify the directory where the packages are located. But when I use "javac -d /home/myname/workspace/HelloWorldSWT/ -classpath /home/myname/workspace/ HelloWorldSWT.java" I still get the old error message. – Roman Feb 17 '10 at 09:50
0

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.

James B
  • 3,692
  • 1
  • 25
  • 34
0

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

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Stephen, I was able to run the application from the Eclipse. And I just managed to compile the class file (by javac -d /home/myname/workspace/HelloWorldSWT/ -cp /home/myname/workspace/org.eclipse.swt/swt.jar). But I still do not know how can I run the generated class file (but it is already another question). – Roman Feb 17 '10 at 10:10
-2

You forgot about classpath

St.Shadow
  • 1,840
  • 1
  • 12
  • 16
  • 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? – Roman Feb 17 '10 at 09:29
  • Yes, you understand me wrongly. Specify classpath exactly via -cp option: javac -cp /jarname.jar;/another_jarname.jar; and so on – St.Shadow Feb 17 '10 at 09:50