0
package com.scheduler.process;

public class Process {
    public enum state {
        NOT_SUBMITTED, SUBMITTED, BLOCKED, READY, RUNNING, COMPLETED 
    }
        private state currentState;

    public state getCurrentState() {
        return currentState;
    }

    public void setCurrentState(state currentState) {
        this.currentState = currentState;
    }

}



package com.scheduler.machine;
import com.scheduler.process.Process;
import com.scheduler.process.Process.state;

public class Machine {
    com.scheduler.process.Process p = new com.scheduler.process.Process();
    state s = state.READY;  //fails if I don't also explicitly import Process.state
    p.setCurrentState(s);  //says I need a declarator id after 's'... this is wrong.
    p.setCurrentState(state.READY);
}

Modified the example to try and direct to the issue. I cannot change the state on this code. Eclipse suggests importing Process.state like I had on my previous example, but this doesn't work either. This allows state s = state.READY but the call to p.setCurrentState(s); fails as does p.setCurrentState(state.READY);

Problem continued.... Following Oleg's suggestions I tried more permutations:

package com.scheduler.machine;
import com.scheduler.process.Process;
import com.scheduler.process.Process.*;

public class Machine {
    com.scheduler.process.Process p = new com.scheduler.process.Process();
    public state s = Process.state.READY;  
    p.setCurrentState(s);
    p.setCurrentState(state.READY);
}

Okay. It's clear now that I'm a candidate for lobotomy.

package com.scheduler.machine;

import com.scheduler.process.Process;
import com.scheduler.process.Process.state;

public class Machine {

    public void doStuff(){
        com.scheduler.process.Process p = new com.scheduler.process.Process();
        state s = state.READY;  //fails if I don't also explicitly import Process.state
        p.setCurrentState(s);  //says I need a declarator id after 's'... this is wrong.
        p.setCurrentState(state.READY);
    }

}

I needed to have a method in the class--but we're still missing something (probably obvious) here. When I go via the command line and run javac on the Machine class AFTER compiling Process, I still get the following error:

mseil@context:/media/MULTIMEDIA/Scratch/Scratch/src/com/scheduler/machine$ javac Machine.java Machine.java:3: package com.scheduler.process does not exist import com.scheduler.process.Process; ^

So I guess the question now becomes, what idiot thing am I missing that is preventing me from compiling this by hand that eclipse is doing for me behind the scene?

====== Problem solved here:
Java generics code compiles in eclipse but not in command line

Community
  • 1
  • 1
avgvstvs
  • 6,196
  • 6
  • 43
  • 74
  • 2
    "Stopped working" is pretty vague. What didn't work as you expected? Can you post a complete 10-line example to demonstrate the problem? – sarnold Nov 11 '11 at 03:45
  • Read the code. When I pulled the project into my Linux instance (running openJDK) the import failed. I can't make that any more clear. – avgvstvs Nov 11 '11 at 15:27
  • `import Process.state;` produces error both on Windows and Linux – Oleg Mikheev Nov 11 '11 at 15:34
  • Altered example to demonstrate that packages don't seem to be the issue here. @Oleg this is true for me as well, now. I will have to dig into eclipse history to find the original code that wouldn't compile across systems. (I stopped using enums and switched to a constants class as a workaround.) – avgvstvs Nov 11 '11 at 16:03
  • -1: Reading the code is only _part_ of the problem -- _you_ have the error message from your compiler; we don't. This is still vague. – sarnold Nov 12 '11 at 00:07
  • @sarnold There is a comment on the line `p.setCurrentState(s);` that says exactly what the compiler warning is. – avgvstvs Nov 12 '11 at 21:01
  • @avgvstvs, thanks for the notice, downvote converted into upvote. :) – sarnold Nov 13 '11 at 00:10
  • I'm losing my hair over this. I tried factoring out the enum... a few new permutations for those who are still inclined to help... – avgvstvs Nov 13 '11 at 05:43
  • are you trying to tell us that calling a class `State` and referring to it as `state` from other classes works for you on Windows? :) – Oleg Mikheev Nov 13 '11 at 07:54
  • No, forget the while windows thing (I deleted "Portability?" from the question some time ago). I can say this, in the main project I pulled this example from, it works fine when running in eclipse--but fails to compile from the command line. The code I have in here now represents the same error (everywhere I run it) as I was getting from javac [-g] Machine.java. (Happens with or without -g flag.) – avgvstvs Nov 13 '11 at 15:01
  • Solution is here: http://stackoverflow.com/questions/8087337/java-generics-code-compiles-in-eclipse-but-not-in-command-line – avgvstvs Nov 16 '11 at 16:07
  • Solution lies here: http://stackoverflow.com/questions/8087337/java-generics-code-compiles-in-eclipse-but-not-in-command-line – avgvstvs Nov 16 '11 at 16:09

2 Answers2

3

This has just worked for me:

  1. Download latest Eclipse
  2. Create new project
  3. Create two packages com.scheduler.process and com.scheduler.machine
  4. Create class Process in package com.scheduler.process and class Machine in com.scheduler.machine and copy their contents from your post modifying them to conform to Java language syntax, like this: enter image description here enter image description here
  5. Everything compiles right away.

------ to answer the previous version of the question ------

To answer the question as it is right now: you need to either import com.scheduler.process.Process.status or import com.scheduler.process.Process.* and refer to status as just status

or

import com.scheduler.process.* or import com.scheduler.process.Process and refer to status as Process.status

------ to answer the original version of the question ------

  1. You can't import classes that are not inside some package. You just can't. It is a compile time error to import a type from the unnamed package.

  2. You don't need to import anything if your classes are in the same package, or if all of your classes are packageless.

  3. If Process class was inside some package it would be possible to import just its status inner class: import a.b.c.Process.status would work just fine.

  4. All your Windows/Linux migration issues don't have anything to do with Java and exceptions that you see. import Process.state; will produce exception on any OS because you can't import classes that don't belong to any package.

Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95
  • They're in the default package at the moment. – avgvstvs Nov 11 '11 at 15:23
  • which means that you don't have to import anything – Oleg Mikheev Nov 11 '11 at 15:33
  • Altered example to demonstrate that packages don't seem to be the issue here. – avgvstvs Nov 11 '11 at 16:03
  • If you put Process to a package everything will just work. The new Process() actually creates kava.lang.Process in your case as it is right now. – Oleg Mikheev Nov 12 '11 at 05:30
  • Oleg, I altered the example to the point of explictly declaring ` com.scheduler.process.Process p = new com.scheduler.process.Process();` and it still won't let me set state. As you can see I moved both pieces of code into their own packages. Unless there's something REALLY messed up in my system, this still doesn't work. – avgvstvs Nov 12 '11 at 17:49
  • Oleg, thanks for your continued help. I tried what you suggested, and more... (factored out the enum into its own class...) Still getting compiler errors. – avgvstvs Nov 13 '11 at 05:47
  • Okay, I obviously derped that one on the example. BUT... I still can't compile Machine from the command line. To copy my question:"What idiot thing am I missing that is preventing me from compiling this by hand that eclipse is doing for me behind the scene?" – avgvstvs Nov 13 '11 at 15:32
1

Eclipse doesn't use the Sun JDK by default. I would assume that you are using Eclipse's built in compiler as Sun's JDK and the OpenJDK are almost identical.

Java code compiles and runs exact the same on Windows and Linux most of the time (unless you use a few of the platform specific operations)

I suspect you are not building the code the same way and when you compile Machine, the Process class has not been compiled.

I suggest you use a standard build system like maven or ant and it will build the same everywhere. Failing that run Eclipse on Linux or just the same .class you use on windows as they don't need to be re-compiled in any case.

BTW: You don't need to import Process.state as it not used and its in the same package (so you wouldn't need to if you did)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130