13

Question(s):

How am I supposed to compile just one class? How do I put it INTO the class file (which I've created)? Doesn't Eclipse just automatically compile all the classes at runtime?

The back story:

I'm following a tutorial and it tells me to:

Put the compiled class into WEB-INF/classes.

Where the class is:

package org.odata4j.tomcat;
import java.util.Properties;
import org.core4j.Enumerable;
import org.core4j.Func;
import org.core4j.Funcs;
import org.odata4j.producer.ODataProducer;
import org.odata4j.producer.ODataProducerFactory;
import org.odata4j.producer.inmemory.InMemoryProducer;

public class ExampleProducerFactory implements ODataProducerFactory {

  @Override
  public ODataProducer create(Properties properties) {
    InMemoryProducer producer = new InMemoryProducer("example");

    // expose this jvm's thread information (Thread instances) as an entity-set called "Threads"
producer.register(Thread.class, Long.class, "Threads", new Func<Iterable<Thread>>() {
  public Iterable<Thread> apply() {
    ThreadGroup tg = Thread.currentThread().getThreadGroup();
    while (tg.getParent() != null)
      tg = tg.getParent();
    Thread[] threads = new Thread[1000];
    int count = tg.enumerate(threads, true);
    return Enumerable.create(threads).take(count);
  }
}, Funcs.method(Thread.class, Long.class, "getId"));

return producer;
  }
 }
dkb
  • 4,389
  • 4
  • 36
  • 54
AllieCat
  • 3,890
  • 10
  • 31
  • 45
  • 3
    `"Doesn't Eclipse just automatically compile all the classes at runtime?"` -- Eclipse automatically compiles as you type. That is one way it can identify compilation errors quickly. If you look in the bin subdirectory, you'll see the class files. – Hovercraft Full Of Eels Jun 21 '13 at 20:31
  • 2
    Usually eclipse does this for you. If you go to the eclipse build path (right click properties on your project -> build path) and then select 'source', it should show an output folder. – Nathaniel Ford Jun 21 '13 at 20:32
  • You can use `javac` to compile any java file you want if its on your path. – thatidiotguy Jun 21 '13 at 20:32
  • 1
    @HovercraftFullOfEels Note that eclipse compiles by default, but needn't. – Nathaniel Ford Jun 21 '13 at 20:33

7 Answers7

14

When you save your .java file, Eclipse will compile it into a .class file if there are not compiler errors. You can usually find this file in the bin subdirectory of your project. In particular, it will be in bin/org/odata4j/tomcat because you have declared your class to belong to the org.odata4j.tomcat package. Feel free to copy this file anywhere you wish.

Note: You should only use org.odata4j in your package name if you own the odata4j.org domain. Otherwise, you should choose your own package name.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Where should the bin subdirectory be? – AllieCat Jun 21 '13 at 20:54
  • 3
    In the root of the project, but it might be a different folder depending on the project configuration. You can check this in the Properties menu, in the Build Path section. – MaQy Jun 21 '13 at 21:34
  • More precisely, there is a "Default output folder" input element when you navigate: Right click on project -> Properties -> Select "Java Build Path" – Victor Feb 25 '19 at 14:01
7

I find my classes files in Project -> Properties -> Java Build Path at field Default output folder. To me the default was 'project-name/target/classes'.

Line
  • 1,529
  • 3
  • 18
  • 42
Mateus Galasso
  • 320
  • 1
  • 5
  • 14
2

your java file location should be: org/odata4j/tomcat/ExampleProducerFactory.java then you can on command line do: javac org/odata4j/tomcat/ExampleProducerFactory.java which will create the compiled class file: org/odata4j/tomcat/ExampleProducerFactory.class put that in a folder WEB-INF/classes/org/odata4j/tomcat/ExampleProducerFactory.class

but better yet, create "Dynamic Web Project" in eclipse, which will take care of everything for you (just use defaults). the end result will be a .war file which you can create by the menu option: file->export

such a .war file can be deployed in any web container such as tomcat. look for an autodeploy directory within your tomcat or use the tomcat managment console to deploy it.

necromancer
  • 23,916
  • 22
  • 68
  • 115
1

After compiling the .java successfully, the .class will be in the bin folder under the project workspace .

Bipin
  • 11
  • 1
0

Assuming this class is in a file called ExampleProducerFactory.java, you can compile it at the command line by navigating to the directory containing the file and typing

javac ExampleProducerFactory.java

This will create a file named ExampleProducerFactory.class which you can move to the desired directory.

Brian
  • 3,091
  • 16
  • 29
  • 1
    Note that if Eclipse automatic build is enabled, it will quickly overwrite the produced class file, if you happen to put that class file in the configured output directory. (such as `WEB-INF/classes`) – Nathaniel Ford Jun 21 '13 at 20:34
  • @Henry Check the edit history. At the time I posted this answer the question was "How to compile a just one Java class?" – Brian Jun 21 '13 at 20:51
  • Vote is locked until you edit your answer. So... If you edit I'll un-downvote. – Dummy Code Jun 21 '13 at 21:04
0

For this to work properly in Eclipse you need to be working in a "Dynamic Web Project" and not in a plain "Java project", which is not available in the standard plain Java download of Eclipse.

You either need download and use the Java EE flavour of Eclipse, or add WTP to your current install to get it.

Note that the absolutely simplest way to get up and running with a simple Web application is to use Netbeans which has this properly wired by default (including a Tomcat) in the Java EE flavour.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
0

I simply open the java file location (in package explorer right click-->Show In -->System Explorer) go to top of the workspace and search for name of the file without extension. it gives me .class file as well. I just delete it.

Amol Patil
  • 238
  • 1
  • 2
  • 7