Reputation: 77
I have a working version of my project in eclipse.
I exported the project as a runnable jar.
Extracted (after converting to .zip) and tried to compile a particular java file from the command prompt
(Doing it this way since I have a project requirement, where input parameter inside that particular file can be modified and recompiled/run by users who wont have Eclipse)
I have used some external libraries (for Eg:json-simple,gson etc).They arent getting recognized , during compilation. But if I run the class file (from the Eclipse compiled version), it gets executed properly
javac packageName.javaFileName.java
javac javaFileName.java
The 1) part didn't compile at all saying classNotFound. The 2) part started compiling but threw an error where none of the external libraries got recognized. I'm getting :
error: cannot find symbol for places wherever the code/import of the external lib is used
Upvotes: 3
Views: 3327
Reputation: 181159
- Tried to compile from root folder (using package name)
javac packageName.javaFileName.java
- Went inside the package and compiled directly.
javac javaFileName.java
Yes. javac
requires you to specify a filesystem path to the (first) source(s) to compile. You appear instead to have tacked .java
onto the end of the desired fully-qualified class name. Probably you want to compile from the root of the unpacked jar, specifying a correct path:
javac [options] package/name/className.java
for class package.name.className. (You can also compile from a different working directory if you specify an appropriate option, as discussed below.)
The 2) part started compiling but threw an error where none of the external libraries got recognized.
If the class you're compiling depends on others that also need to be compiled then javac
would likely make a similar complaint about them. Either compile from the root (as in (a)), or specify the path to the source root via the -sourcepath
option. Either way, there's no reason to descend into the source tree to compile.
But the external libs are actually a separate, albeit related, question. You don't need to compile these, but you do need to tell javac
to use them as sources of classes. You would do that via the -classpath
option, which you can abbreviate to -cp
. If those were packaged in the jar itself (i.e. a "fat jar") then that should be fairly easy, something along these lines:
javac -cp .:lib/dependency1.jar:lib/dependency2.jar package/name/className.java
The "lib" part may vary, and the separator definitely differs depending on OS (on Windows it is ;
, whereas on Mac / Linux / Solaris is is :
, as shown).
If the external libs were not packaged into the main jar then the procedure is the same, but you might have a bigger challenge finding the needed jars. Also, such a jar is probably not runnable if you move it to a different machine. Nevertheless, you should probably look in META_INF/MANIFEST.MF, as it should contain the needed information.
Upvotes: 1