Compiling java code cannot find source files - java

I am following exercise 1.2 of the Oracle Java SE 8 exam preparation which instructs me to build a main class GreetingsUniverse and three classes which will be initialised by the main class; Mars, Earth and Venus.
I have the com directory stored at location: C:\Users\Chris\Documents\JavaMM
package com.ocajexam.tutorial;
import com.ocajexam.tutorial.planets.Earth;
import com.ocajexam.tutorial.planets.Venus;
import com.ocajexam.tutorial.planets.Mars;
public class GreetingsUniverse
{
public static void main (String[] args)
{
System.out.println("Greetings, Universe!");
Earth e = new Earth();
Mars m = new Mars();
Venus v = new Venus();
}
}
package com.ocajexam.tutorial.planets;
public class Mars
{
public Mars()
{
System.out.println("Hello from Mars!");
}
}
package com.ocajexam.tutorial.planets;
public class Earth
{
public Earth()
{
System.out.println("Hello from Earth!");
}
}
package com.ocajexam.tutorial.planets;
public class Venus
{
public Venus()
{
System.out.println("Hello from Venus!");
}
}
The exercise aim is to determine the command line arguments required to compile the code.
I have the GreetingsUniverse java file stored in com\ocajexam\tutorial\ and the planets store in com\ocajexam\tutorial\planets
I have tried various solutions with no success:
javac -d . -cp com\ocajexam\tutorial\planets com\ocajexam\tutorial\GreetingsUniverse.java
javac -d . -cp com com\ocajexam\tutorial\GreetingsUniverse.java
I keep getting the following error:
C:\Users\Chris\Documents\JavaMM>javac -d . -cp com com\ocajexam\tutorial\GreetingsUniverse.java
com\ocajexam\tutorial\GreetingsUniverse.java:8: error: cannot find symbol
Earth e = new Earth();
^
symbol: class Earth
location: class GreetingsUniverse
com\ocajexam\tutorial\GreetingsUniverse.java:8: error: cannot find symbol
Earth e = new Earth();
^
symbol: class Earth
location: class GreetingsUniverse
com\ocajexam\tutorial\GreetingsUniverse.java:9: error: cannot find symbol
Mars m = new Mars();
^
symbol: class Mars
location: class GreetingsUniverse
com\ocajexam\tutorial\GreetingsUniverse.java:9: error: cannot find symbol
Mars m = new Mars();
^
symbol: class Mars
location: class GreetingsUniverse
com\ocajexam\tutorial\GreetingsUniverse.java:10: error: cannot find symbol
Venus v = new Venus();
^
symbol: class Venus
location: class GreetingsUniverse
com\ocajexam\tutorial\GreetingsUniverse.java:10: error: cannot find symbol
Venus v = new Venus();
^
symbol: class Venus
location: class GreetingsUniverse
6 errors
Does anyone know where I am going wrong with this?
I have also tried the following which produces a different error message:
C:\Users\Chris\Documents\JavaMM>javac -d . com\ocajexam\tutorial\planets\Earth.java;com\ocajexam\tutorial\planets\Mars.java;com\ocajexam\tutorial\planets\Venus.java com\ocajexam\tutorial\GreetingsUniverse.java
Produces this error:
javac: file not found: com\ocajexam\tutorial\planets\Earth.java;com\ocajexam\tutorial\planets\Mars.java;com\ocajexam\tutorial\planets\Venus.java
Usage: javac <options> <source files>
use -help for a list of possible options
The files are definitely there, if I dir on the planets directory I can see them there:
Directory of C:\Users\Chris\Documents\JavaMM\com\ocajexam\tutorial\planets
16/10/2016 11:33 <DIR> .
16/10/2016 11:33 <DIR> ..
15/10/2016 21:48 135 Earth.java
15/10/2016 21:48 132 Mars.java
15/10/2016 21:48 135 Venus.java
Update:
After further debugging this is just getting stranger..
I am trying to compile only the planet files using the first half of the command:
javac -d . -cp com\ocajexam\tutorial\planets\*.java
When I check the planet directory, only two of the planets have compiled. When I remove the java class that isn't compiling, only 1 of the files is compiled!
When I use the same command specifying the java file I want to compile:
javac -d . -cp com\ocajexam\tutorial\planets\Venus.java
I get the following error:
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options
I can't make any sense of this because Venus.java was one of the files that did compile when I ran the /*.java compilation command to detect any java files in the directory

You need to compile all the classes first. The javac only compiles your main class. Use javac -cp /path/to/file/*.java. The asterisk (*.java) means to compile all java classes in the folder.

The example in the OCA Java SE 8 Programmer I Study Guide uses a constructor without parentheses.
package com.ocajexam.tutorial.planets;
public class Earth {
public Earth {
System.out.println("Hello from Earth!");
}
}
this should, of course, be
package com.ocajexam.tutorial.planets;
public class Earth {
public Earth () {
System.out.println("Hello from Earth!");
}
}
You can then run javac com/ocajexam/tutorial/greetingsUniverse.java and java com/ocajexam/tutorial/GreetingsUniverse.
Although strictly speaking this is not an answer to the question (which uses constructors with parentheses), it might help other people with the same error.

All of the .java files used need to be on the classpath. ie javac -d . -cp com com\ocajexam\tutorial\*.java com\ocajexam\tutorial\planets\*.java
Try doing: javac -d . -cp com com/ocajexam/tutorial/GreetingsUniverse.java com/ocajexam/tutorial/planets/Earth.java com/ocajexam/tutorial/planets/Mars.java com/ocajexam/tutorial/planets/Venus.java

Related

javac adding classpath breaks my local class compilation

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
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.

Compiling java class with javac doesn't work

I want to compile my java class like that: javac ResultSet.java
But I get the following error:
ResultSet.java:5: error: package data does not exist
import data.Spieler;
^
ResultSet.java:8: error: cannot find symbol
private ArrayList<Spieler> meineSpieler = new ArrayList<Spieler>();
^
symbol: class Spieler
location: class ResultSet
ResultSet.java:12: error: cannot find symbol
public native Spieler[] getSpieler();
^
symbol: class Spieler
location: class ResultSet
ResultSet.java:18: error: cannot find symbol
public ArrayList<Spieler> getMeineSpieler() {
^
symbol: class Spieler
location: class ResultSet
ResultSet.java:8: error: cannot find symbol
private ArrayList<Spieler> meineSpieler = new ArrayList<Spieler>();
^
symbol: class Spieler
location: class ResultSet
How can I import the spieler class? Should I set the classpath or is there a other way to fix that?
Go one directory up and then compile it with
javac data/JNIResultSet.java
Update:
Ok, your class JNIResultSet is in package model and it uses other classes in package data.
Then your compile command should be as follows:
javac -cp . model/JNIResultSet.java
The -cp . part means, that your classpath includes the current directory. This is the root of your package hierarchy. So the compiler can find the *.java files in package data and compiles them also as needed.
You see, this can be very complicated. For more classes this will be nearly unmanageable. So you should really consider to use a build system like Ant, Maven or Gradle.
use -classpath while compiling the file as
javac -classpath <path-to-dependent-classes> JNIResultSet.java
It is required only when the Spieler is not on classpath!
for more help refer javac oracle documentation

javac's recognition of packages

I have a package LMath with a class LMatrix. LMatrix has a method public LMatrix getInverse() that throws LDimensionException.
The first line in both of these files is:
package com.kavricious.LMath;
Compiling this class in jGrasp results in no problem, but if I enter
PS C:\programming\java\javaprojects\com\kavricious\lmath> javac LMatrix.java
in Windows PowerShell, the stack trace reads:
LMatrix.java:70: error: cannot find symbol
public LMatrix getInverse() throws LDimensionException{
^
symbol: class LDimensionException
location: class LMatrix
how do I tell javac to recognize members as in the same package?
C:\programming\java\javaprojects\com\kavricious\lmath> javac LMatrix.java
That should be
C:\programming\java\javaprojects> javac com\kavricious\LMath\LMatrix.java
And similarly for all other Java files: compile from the root of the package hierarchy, and name the entire path to the .java file. Then the object files will be put in the right place, and found, and the ither .java files will be compiled as necessary.

Javac can't find package referenced in classpath

I'm trying to compile a project with javac in windows, but I'm getting a "package x does not exist" error.
Even if the jar file containing them is in the classpath.
Here is the command, I just added line returns to make it readable:
javac
-d bin
-sourcepath src
-cp
.;
lib/gson-2.5.jar;
lib/jruby-complete-9.1.2.0.jar;
lib/lwjgl-platform-2.9.3-natives-windows.jar;
lib/lwjgl-platform-2.9.3-natives-linux.jar;
lib/lwjgl-platform-2.9.3-natives-osx.jar;
lib/jinput-platform-2.0.5-natives-windows.jar;
lib/jinput-platform-2.0.5-natives-linux.jar;
lib/jinput-platform-2.0.5-natives-osx.jar;
lib/lwjgl.jar;
lib/lwjgl_util.jar;
lib/jorbis-0.0.17.jar;
lib/jinput-2.0.5.jar;
lib/gdx-platform-1.9.2-natives-desktop.jar;
lib/gdx-controllers-platform-1.9.2-natives-desktop.jar;
lib/gdx-freetype-platform-1.9.2-natives-desktop.jar;
lib/gdx-1.9.2.jar;lib/gdx-backend-lwjgl-1.9.2.jar;
lib/gdx-controllers-1.9.2.jar;
lib/gdx-controllers-desktop-1.9.2.jar;
lib/gdx-freetype-1.9.2.jar;
lib/jlayer-1.0.1-gdx.jar;
lib/jutils-1.0.0.jar
src/com/azias/awbe/Launcher.java
And here is the error message:
src\com\azias\awbe\Launcher.java:3: error: package com.badlogic.gdx.backends.lwjgl does not exist
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
^
src\com\azias\awbe\Launcher.java:4: error: package com.badlogic.gdx.backends.lwjgl does not exist
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
^
src\com\azias\awbe\Launcher.java:16: error: cannot find symbol
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
^
symbol: class LwjglApplicationConfiguration
location: class Launcher
src\com\azias\awbe\Launcher.java:16: error: cannot find symbol
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
^
symbol: class LwjglApplicationConfiguration
location: class Launcher
src\com\azias\awbe\Launcher.java:27: error: cannot find symbol
new LwjglApplication(new AdvanceWarsBootleg(), config);
^
symbol: class LwjglApplication
location: class Launcher
src\com\azias\awbe\AdvanceWarsBootleg.java:3: error: package com.badlogic.gdx does not exist
import com.badlogic.gdx.Game;
^
src\com\azias\awbe\AdvanceWarsBootleg.java:4: error: package com.badlogic.gdx.graphics.g2d does not exist
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
^
src\com\azias\awbe\AdvanceWarsBootleg.java:6: error: cannot find symbol
public class AdvanceWarsBootleg extends Game {
^
symbol: class Game
src\com\azias\awbe\AdvanceWarsBootleg.java:7: error: cannot find symbol
public SpriteBatch batch;
^
symbol: class SpriteBatch
location: class AdvanceWarsBootleg
src\com\azias\awbe\AdvanceWarsBootleg.java:9: error: method does not override or implement a method from a supertype
#Override
^
src\com\azias\awbe\AdvanceWarsBootleg.java:17: error: method does not override or implement a method from a supertype
#Override
^
src\com\azias\awbe\AdvanceWarsBootleg.java:19: error: cannot find symbol
super.render();
^
symbol: variable super
location: class AdvanceWarsBootleg
12 errors
Using Java 6 or later, the classpath option supports wildcards. Note the following:
Use straight quotes (")
Use *, not *.jar
so you could simplify your javac statement:
javac
-d bin
-sourcepath src
-cp ".;libs/*"
src/com/azias/awbe/Launcher.java
Also depending on the platform the separator is ; (windows) or : (unix).

Java can't find classes in parent package

I'm not sure if this is a classpath problem, a syntax problem, or an access modifier problem. I'm trying to implement packages for the first time in Java and having with the compiler not finding classes in the parent package.
I understand there isn't any hierarchical relationship in package structures and I am explicitly importing parent package classes in the child package class.
The parent package classes' constructors are public.
I am under the impression both directories need to be on the classpath but not sure about it. Either way, I have both dirs on the classpath to be sure.
Directory Structure
home
|
|---java
|
|---src
|
|---com
|
|---inv
|
|---mail
|
|---SendMail.java
|
|---TeradataCon.java
|
|---ExcelWriter.java
CLASSPATH
(mdexter#server) /home/mdexter/java/src/com/inv/mail # echo $CLASSPATH
.:/storage/mdexter/java/lib/*:/usr/java6_64/jre/lib/*:/usr/java6_64/lib/*:/home/mdexter/java/src/com/inv/*:/home/mdexter/java/src/com/inv/mail/*
SendFile.java (stripped down)
package com.inv.mail;
import com.inv.TeradataCon;
import com.inv.ExcelWriter;
public class SendMail
{
public static void main(String[] args)
{
TeradataCon teradata = new TeradataCon(some, args, here);
ExcelWriter xls = new ExcelWriter(some, args, here);
}
}
TeradataCon.java (stripped down)
package com.inv;
public class TeradataCon
{
public TeradataCon()
{
// stuff
}
}
ExcelWriter.java (stripped down)
package com.inv;
public class ExcelWriter
{
public ExcelWriter()
{
// stuff
}
}
Error output
(mdexter#server) /home/mdexter/java/src/com/inv/mail # javac *.java
StrategyVolumes.java:3: cannot find symbol
symbol : class TeradataCon
location: package com.inv
import com.inv.TeradataCon;
^
StrategyVolumes.java:4: cannot find symbol
symbol : class ExcelWriter
location: package com.inv
import com.inv.ExcelWriter;
^
StrategyVolumes.java:14: cannot find symbol
symbol : class TeradataCon
location: class com.inv.mail.StrategyVolumes
TeradataCon teradata = new TeradataCon(
^
StrategyVolumes.java:14: cannot find symbol
symbol : class TeradataCon
location: class com.inv.mail.StrategyVolumes
TeradataCon teradata = new TeradataCon(
^
StrategyVolumes.java:32: cannot find symbol
symbol : class ExcelWriter
location: class com.inv.mail.StrategyVolumes
ExcelWriter xls = new ExcelWriter(;
^
StrategyVolumes.java:32: cannot find symbol
symbol : class ExcelWriter
location: class com.inv.mail.StrategyVolumes
ExcelWriter xls = new ExcelWriter(;
^
6 errors
What I have tried
import com.inv.*; (Shouldn't matter right?)
Compiled parent classes from /home/java/src/com/inv - works
Compiled mail/*.java from /home/java/src/com/inv - doesn't work
I think you've misunderstood the classpath, for starters. You don't put package directories on the classpath - you only put the root of output directories there.
I suggest you compile from the src directory, with the output going to a bin or classes directory. For example, get rid of your CLASSPATH environment variable entirely (it's rarely useful, IME - better to specify it as a command-line option where necessary) and then use something like:
/home/mdexter/java/src # javac -d ../bin com/inv/mail/*.java
Or better, compile everything together, as JB Nizet suggests:
/home/mdexter/java/src # javac -d ../bin `find . -name '*.java'`
(Or use an IDE and/or build tool.)

Categories

Resources