3

I'm using IntelliJ for java development. I want to run my application on another host which I only have shell access to.

When I run my application locally, it's all fine and dandy. When I attempt to compile the code on the remote host, I get:

usr@host1:~/mp1/src$ javac -cp ../ Control.java
Control.java:10: error: cannot find symbol
    private static Node genNode = null;
                   ^
  symbol:   class Node
  location: class Control
Control.java:25: error: cannot find symbol
            genNode = new Node(hostname);
                          ^
  symbol:   class Node
  location: class Control
2 errors

The file Node.java is in the same directory:

mp1
├── src
│   ├── ClientControl.java
│   ├── Control.java
│   ├── Node.java
│   ├── Registrar.java
│   ├── ServerControl.java
│   ├── UX.java
├── lib
│   └── kryonet-2.21-all.jar

I can't compile locally either, unless it's from within the IDE, so I assume I'm just not doing something right. What am I missing? I just want to be able to run my application from a shell, I don't really care how.

MrDuk
  • 16,578
  • 18
  • 74
  • 133
  • 2
    probably you have a library in your project in your IDE, you don't have in your basic lib folder, so the compiler can't find it – Stultuske Sep 09 '15 at 06:28
  • Hm, maybe so? I added my lib tree to the post.. any idea how to fix it so the compiler can see it? – MrDuk Sep 09 '15 at 06:30
  • just check have you configured your `JAVA_HOME` to test just try command `echo %JAVA_HOME%` – SaviNuclear Sep 09 '15 at 06:30
  • Check which libraries you added in the ide, and add those to your lib of your basic Java folder – Stultuske Sep 09 '15 at 06:31
  • @SaviNuclear - hm, `echo $JAVA_HOME` displays nothing. – MrDuk Sep 09 '15 at 06:32
  • 2
    Please show a short but *complete* program demonstrating the problem. We don't know what packages your code is in or anything like that. You could try just compiling all your code at once: `javac -cp .. *.java` – Jon Skeet Sep 09 '15 at 06:32
  • You need to add the libraries to the classpath. Use `javac -cp *.java` – Codebender Sep 09 '15 at 06:33
  • You need to add any library you are using to the compiler classpath using the `-cp` parameter. Please see the javac manual: http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html#userclasspath –  Sep 09 '15 at 06:34
  • @JonSkeet -- when I try to compile all at once, I just get `Error: Could not find or load main class ClientControl.java` – MrDuk Sep 09 '15 at 06:34
  • 1
    @MrDuk: That sounds like an error when you *run* it (due to not running it properly - you don't specify a `.java` file when trying to run the code, you just specify the class name). – Jon Skeet Sep 09 '15 at 06:36

2 Answers2

2

You have to correctly set the classpath when you compile.

There is a problem in your classpath. Since you have set only the parent directory as your classpath. You have to include src too, since it contains your other source files.

Try:

javac  -cp .:../lib/kryonet-2.21-all.jar Control.java
Amila
  • 5,195
  • 1
  • 27
  • 46
  • Cool, this looks like it built the `.class` files properly. Now I've no clue how to execute my application. I've tried `java UX.java`, which contains my `main()`, but it just says `Could not find or load main class UX.java` – MrDuk Sep 09 '15 at 06:44
  • 1
    You execute the application with complied bytecode (.class), not source files. Again, correctly include all your complied classes and libraries in the classpath. – Amila Sep 09 '15 at 06:46
  • 1
    Try java -cp .:../lib/kryonet-2.21-all.jar UX – Amila Sep 09 '15 at 06:46
  • Bingo! thanks a bunches. Bonus points - is my project laid out wonky? Should I have this organized differently, or is it common to have to specify this stuff at the cli? – MrDuk Sep 09 '15 at 06:47
  • 1
    Your structure is fine for a simple/learning project. In practice, "java/javac" are rarely used directly in Java world. You either use IDE or use build tools like Maven. – Amila Sep 09 '15 at 06:55
0

Find /usr/lib/jvm/java-1.x.x-openjdk

vim /etc/profile

add these two lines:

export JAVA_HOME="path that you found"

export PATH=$JAVA_HOME/bin:$PATH

For more reference you can try:- How to set JAVA_HOME in Linux for all users

Community
  • 1
  • 1
SaviNuclear
  • 886
  • 1
  • 7
  • 19