0

When I import a package to my MyLib class (which requires -cp to javac) I can no longer compile my MyMain class.

MyMain.java:

class MyMain
{
    public static void main (String [] args)
    {
        MyLib.do_stuff ();
    }
}

MyLib.java:

import com.google.gson.JsonElement;

class MyLib
{
    public static void do_stuff ()
    {
        System.out.println ("Hello.");
    }
}

When I javac MyLib.java I have do do it like this

javac -cp GSON_JAR_PATH MyLib.java

That works but if I

javac MyMain.java

I get

./MyLib.java:1: error: package com.google.gson does not exist
import com.google.gson.JsonElement;

but if I add -cp to the compilation command

javac -cp GSON_JAR_PATH MyMain.java

I get

MyMain.java:5: error: cannot find symbol
                MyLib.do_stuff ();
                ^
  symbol:   variable MyLib
  location: class MyMain
2
  • Nope. I still get "cannot find symbol MyLib" either with -cp .,PATH or -cp . -cp PATH. Commented Feb 17, 2020 at 13:19
  • That did it, thanks. Want to make an actual answer so I can accept it? Commented Feb 17, 2020 at 13:25

1 Answer 1

0

Use "-cp path1:path2" - colon separated. (semicolon works on windows) (the parameter to cp is quoted....

javac -cp path1:path2 //or ; for windows.

Note 1 - setting -cp overrides any existing CLASSPATH environment or default path setting.

Note 2 - if no CLASSPATH setting then default is '.' - until the -cp overrides that.

So in the posted case - the "." was set for path (either CLASSPATH or default) up until the -cp was used which overrode that default - so it needs to be added back in.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.