0

In my java project I have three java classes: 1. App.java 2. UrlLibrary.java 3. MyIterator.java

Firstly, when the java files are in a default package then App.java has been compiling and running fine in both eclipse and cmd.

But, When I set the java files in a custom package it's running in eclipse fine but fails when I tried to compile it in cmd. The error I'm getting:-

enter image description here

Most probably, the error is occurring because I am using another class in the App.java class which I'm actually compiling through javac. AS the error refers:

UrlLibrary sportsurl= new UrlLibrary(sports);

But that should not be the problem since all the three java files are in the same package and it's running fine in eclipse. So, what could be the reason for this scenario?

My Folder Structure:

enter image description here

And my package:

enter image description here

  • My guess is that your folder structure does not match the package name. Can you show us the folder structure and package name? – Tim Biegeleisen Jun 17 '17 at 08:25
  • @TimBiegeleisen ... edited and shown the folder structure and package name. –  Jun 17 '17 at 08:36
  • Possible duplicate: https://stackoverflow.com/questions/25025018/javac-classpath-option-with-multiple-jar-files-in-current-directory-causing-erro – Tim Biegeleisen Jun 17 '17 at 08:43
  • Instead of compiling from the `com\era` directory, compile from the `src` directory - `javac -d ... com\era\App.java` - or better, pass *all* the source files to javac instead of just one of them. – Jon Skeet Jun 17 '17 at 08:57
  • Compile from src directory doesn't work. And how can i pass all the source file to javac ? –  Jun 17 '17 at 09:09

1 Answers1

0

The syntax of javac is-

javac <options> <source files> 

As, you used -d as a options will specify where to store thee generated class files. So, you're using class folder in your desktop to store the generated class files. That's fine.

But, the problem is for the <source files> you're using App.java and you're compiling from the package directory rather you should compile from src directory. Moreover, according to the package structure you shared, App.java is in com.era package. So, you have to specify the package name with the class to compile properly

javac -d (Path of the directory for storing generated class files) com/era/App.java

Hope this will work. And for run use this command:-

java -cp (Same directory where generated file exists) com.era.App

Here, -cp is for class path. You could also set your desired class path using -cp in the javac as well. Otherwise, the default class path of the environment variable will be set. For more <options> to use in java or javac, run javac/java -help in cmd. You'll get a detailed list of <options> there.

Could be handy: Compile and Run Java Program in package from command line

S.Rakin
  • 812
  • 1
  • 11
  • 24