I am trying to compile (from the command line) a java package that imports another package of my own. I was following a tutorial online but it seems that I get an error when I try to compile the final java file (CallPackage.java).
Here is the file structure:
+ test_directory (contains CallPackage.java)
-> importpackage
-> subpackage (contains HelloWorld.java)
Here is CallPackage.java:
/// CallPackage.java
import importpackage.subpackage.*;
class CallPackage{
public static void main(String[] args){
HelloWorld h2=new HelloWorld();
h2.show();
}
}
and here is HelloWorld.java:
///HelloWorld.java
package importpackage.subpackage;
public class HelloWorld {
public void show(){
System.out.println("This is the function of the class HelloWorld!!");
}
}
Attempted Steps
Go to the subpackage and compile HelloWorld.java with $javac HelloWorld.java.
Go to test_directory and compile CallPackage.java with $javac CallPackage.java.
This gives me an error on the last command:
CallPackage.java:1: package importpackage.subpackage does not exist
import importpackage.subpackage.*;
^
CallPackage.java:4: cannot find symbol
symbol : class HelloWorld
location: class CallPackage
HelloWorld h2=new HelloWorld();
^
CallPackage.java:4: cannot find symbol
symbol : class HelloWorld
location: class CallPackage
HelloWorld h2=new HelloWorld();
^
3 errors
How can I compile both packages? Thanks so much for any help!
The issue was that the class path needs to be set for each command (javac and java):
Attempted Steps
instead of going to subpackage, compile HelloWorld.java from the top_level:
$javac -cp . importpackage/subpackage/HelloWorld.java
compile CallPackage.java in the same way:
$javac -cp . CallPackage.java
run the file using the class path also:
$java -cp . CallPackage
NOTE: running "$java CallPackage" will give an error "Error: Could not find or load main class CallPackage"
In summary, during each step, the class path must be specified. It worked after running it as such.
Same situation to me. And I came to take over it by compiling classes at the same time.
For example, here is my project:
+ beerV1
-> classes
-> src
-> com
-> example
-> model
-> BeerExpert.java
-> web
-> BeerSelect.java
BeerExpert.java:
package com.example.model;
import ...
public class BeerExpert{
...
}
BeerSelect.java:
package com.example.web;
import com.example.model.*;
import ...
public class BeerSelect {
...
}
As you can see: BeerSelect.java is trying to import classes in com.example.model package.
At the first time, I compiled BeerExert.java first by command:
--> javac -d classes src/com/example/model/BeerExpert.java
Then:
--> javac -d classes src/com/example/web/BeerSelect.java
And the result was:
-->... error: package com.example.model does not exist
So, I knew that compiling multiple classes separately will not work in this case.
After suffering on google, I found this very simple way to solve the problem:
Just compile all at once:
--> javac -d classes src/com/example/model/BeerExpert.java src/com/example/web/BeerSelect.java
Finally, here is what I got:
+ beerV1
-> classes
-> com
-> example
-> model
-> BeerExpert.class
-> web
-> BeerSelect.class
-> src
-> com
-> example
-> model
-> BeerExpert.java
-> web
-> BeerSelect.java
Hope that helps.
Are you sure importpackage/subpackage is in your classpath?
-cp path or -classpath path
Specify where to find user class files, and (optionally) annotation processors and source files. This class path overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory. See Setting the Class Path for more details.
If the -sourcepath option is not specified, the user class path is also searched for source files.
If the -processorpath option is not specified, the class path is also searched for annotation processors.
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
It's an old topic but Google has led me to this site. For the completness, I'd like to add one bit to #Vĩnh Thụy Trần's answer on how to run the main class after compiling it into a custom folder. While it looks trivial now it took me some time to get it right.
In order to run your project, you also need to specify a path to the classes:
java -classpath <directory> your.package.name.classname
or
java -cp <directory> your.package.name.classname
Taking Vĩnh Thụy Trần's example again, the command would look like this:
java -cp classes com.example.web.BeerSelect
I hope it will help someone as I spent some time figuring it out.
(1)first compile the code
javac -d importpackage.subpackage.HelloWorld
(2) and then compile the CallPackage.java
javac CallPackage.java
delete your package folder (after pasting you code to some other folder) and then locate to the folder in cmd where you current code is and try javac -d . Helloworld.java (this will create the Helloworldclass and subpackage as well) and try same for you mainfunction code ie Callpackage.java after compiling to run the code try java Callpackage
Related
When I try to compile with javac .Main
D:\Desktop\Development\Java\Section 4\Abstract
❯ javac -classpath . *.java
error: Invalid filename: *.java
Usage: javac <options> <source files>
use --help for a list of possible options
[17:37] Shell xUSAGE 174ms
Main.java:5: error: cannot find symbol
Student st1 = new Student("John");
^
symbol: class Student
location: class Main
Main.java:5: error: cannot find symbol
Student st1 = new Student("John");
^
symbol: class Student
location: class Main
2 errors
But I able to compile it succesfully with
D:\Desktop\Development\Java\Section 4\Abstract\src\com\Testing
>javac -classpath . *.java
Although I still haven't figured it out how to run the Main class
java Main
Error: Could not find or load main class Main
Caused by: java.lang.NoClassDefFoundError: com/Testing/Main (wrong name: Main)
If you want to use straight javac to compile (really, don't - use a build system), you have to specify the files where they are on the file system. so not *.java, but src\com\Testing\*.java. Yes, if you have many packages, this means an extremely large command line.
java's argument takes a class name - a fully qualified one. Not a path.
If you have this source file:
package com.foo;
public class Bar {
public static void main(String[] args) { .. }
}
Then:
It should be in projectRoot/src/main/java/com/foo/Bar.java where projectRoot is whatever dir serves as your main project folder, src/main/java can be just src, or src/whatever - or something to indicate the kind of sub-project this covers, com/foo matches the package statement, and Bar matches the public class name.
When compiling a Bar.class falls out of it; this should be in projectRoot/bin/com/foo/Bar.class or similar, where bin might also be build. But the com/foo/Bar.class part is mandatory - class files have to be in a subdir structure that matches package.
To run this file, you would put projectRoot/bin on the classpath and then write out the full classname. So, if you're in the projectDir right now, java -cp bin com.foo.Bar.
I'm trying to understand the inclusion of third party jar files in a java project using only the command line in Windows 10.
Specifically, I try to include the file json-20200518.jar in my "project" so that I can use the java object JSONObject in the project.
My java file:
package com.mypackage.example;
import org.json.JSONObject;
class Example {
public static void main(String[] args) {
// ... program logic
}
}
location of my java file (Examp.java):
./com/mypackage/example
location of jar file:
./jars
using cmd win10 I compile:
javac -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" "C:\Users\pfort\Desktop\java\com\mypackage\example\Examp.java"
compilation is successful.
Run:
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" com.mypackage.example.Examp
I get a report:
Error: Could not find or load main class com.mypackage.example.Pokus
Caused by: java.lang.ClassNotFoundException: com.mypackage.example.Pokus
Second attempt:
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" "C:\Users\pfort\Desktop\java\com\mypackage\example\Pokus"
But the same error message comes back to me.
Where am I going wrong? Is it the wrong structure? I don't get it, the compilation is successful but the run does not work.
The compiled Examp.class file isn't part of json-20200518.jar, so you'll need to add the directory containing it to the command line. Assuming it's the current directory (.):
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar;." com.mypackage.example.Examp
From the Definitive ANTLR4 reference I have run through the first example and it has generated the JAVA target. In the directory C:\JavaLib I have antlr-4.5-complete.jar
When I attempt to compile it with;
javac -classpath C:\JavaLib *.java
It creates the following error messages;
helloBaseListener.java:13: error: class HelloBaseListener is public, should be declared in a file named HelloBaseListener.java
public class HelloBaseListener implements HelloListener {
^
helloListener.java:9: error: class HelloListener is public, should be declared in a file named HelloListener.java
public interface HelloListener extends ParseTreeListener {
^
helloParser.java:12: error: class HelloParser is public, should be declared in a file named HelloParser.java
public class HelloParser extends Parser {
^
helloBaseListener.java:3: error: package org.antlr.v4.runtime does not exist
import org.antlr.v4.runtime.ParserRuleContext;
^
helloBaseListener.java:4: error: package org.antlr.v4.runtime.misc does not exist
import org.antlr.v4.runtime.misc.NotNull;
^
helloBaseListener.java:5: error: package org.antlr.v4.runtime.tree does not exist
import org.antlr.v4.runtime.tree.ErrorNode;
....
What am I doing wrong?
There was 2 problems. One was the file has to be named "Hello.g4" not "hello.g4" because the grammar is specified as Hello. The second was the classpath, it requires the path and name of the jar file, as well as the current directory. The following command worked;
javac -classpath .;C:\JavaLib\antlr-4.5-complete.jar *.java
With regard to the above query re the colon separator then the answer is yes. I installed via the debian packages and used the command before working out how to set CLASSPATH
javac -classpath /usr/share/java/antlr4-runtime.jar Expr*.java
Before this I got a load of compile errors. Also it seems to be worth noting on debian at the moment my .bash_profile never gets loaded so I needed to put this in .bashrc
I type javac helloworld.java at cmd in win 7.
C:\Users\User\Downloads\java_tut>javac HelloWorld.java
C:\Users\User\Downloads\java_tut>dir *.class
Directory of C:\Users\User\Downloads\java_tut
03/28/2014 05:42 PM 429 YourClassName.class
C:\Users\User\Downloads\java_tut>
I searched the following directories for helloworld.class:
java, jre, jdk, java_tut, jre/bin, jdk/bin, and my entire harddrive.
I did need to manually add the /jdk/bin directory to my path. I wonder if that matters?
Another possible reason is an empty .java source file.
This causes javac to silently produce nothing.
I experienced that effect with a Hello Word program and a Macintosh editor - which would save with Cmd-S, but does not save with Ctrl-S. I learned this after 20 years of Java programming.
If HelloWorld.java compiled without any errors, then the file HelloWorld.class should definitely be in the java_tut directory.
EDIT (based on your comments and edits):
Check if your Java source file HelloWorld.java looks as follows:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hellow, World!");
}
}
The class must have the same name as the Java source file or you get following compiler error message:
[515]% javac HelloWorld.java
HelloWorld.java:1: error: class YourClassName is public, should be declared in a file named YourClassName.java
public class YourClassName {
^
1 error
Although I asked about the package declaration, I can tell you the correct approach:
Let's assume you have a Java class with that source:
package my.test;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World.");
}
}
Assuming your project root directory is
C:\Projects\java_tut
you must put the source file HelloWorld.java into the directory
C:\Projects\java_tut\my\test
Afterwards you compile and start this little program while being in the java_tut directory with the following commands:
C:\Projects\java_tut> javac my/test/HelloWorld.java
C:\Projects\java_tut> dir my\test
[...]
28.03.2014 09:35 <DIR> .
28.03.2014 09:35 <DIR> ..
28.03.2014 09:35 434 HelloWorld.class
28.03.2014 09:34 134 HelloWorld.java
[...]
C:\Projects\java_tut> java my.test.HelloWorld
Hello World.
Explanation: If working with packages (and you always should use packages for your classes) you must not "sit" in that package, but always run the commands from outside the package (folder).
YourClassName.class is the correct file in this case. The class name isn't generated based on the .java file's name. It's generated based on the class name inside the .java file. In my .java file, I named the class YourClassName and not HelloWorld.
I have this directory structure:
project1/src/edu/course/firstweek/javacourse/Program1.java
Another file in one package above:
project1/src/edu/course/firstweek/program2.java
In the header of program2.java, I have
package edu.course.firstweek;
import edu.course.firstweek.javacourse.Program1;
Now to when I run the following in commandline:
Javac src/edu/course/firstweek/program2.java, I get this error:
src/edu/course/firstweek/program2.java:14:error cannot find symbol
System.out.println(program1.print("hello world"));
symbol: variable Program1
location: class program2
2 errors
I can see that the compiler is not able to find the program1,but I have the correct import package statement in program2. I need help here and after compiling, is there something that needs to be taken into account for running the program.
Thanks
Try going into one directory inside, i.e. cd src
and then compile Javac edu/course/firstweek/program2.java
for running, do java edu.course.firstweek.program2