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

12
  • 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, 2011 at 3: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, 2011 at 15:27
  • import Process.state; produces error both on Windows and Linux Nov 11, 2011 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, 2011 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, 2011 at 0:07

2 Answers 2

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.

7
  • They're in the default package at the moment.
    – avgvstvs
    Nov 11, 2011 at 15:23
  • which means that you don't have to import anything Nov 11, 2011 at 15:33
  • Altered example to demonstrate that packages don't seem to be the issue here.
    – avgvstvs
    Nov 11, 2011 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. Nov 12, 2011 at 5: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, 2011 at 17:49
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.