Other similar answers on StackOverflow do not exactly answer the question.
I am running Windows and have two folders: lib and src. lib contains .jar files for dependencies. I attempted to compile via:
javac -cp ".;lib/*" *.java
It worked. Each file has a .class file and the .java file with the main class has an additional Controller$1.class named file.
I attempted to run the program via all of the following:
java -cp ".;lib/*" src/Controller
java -cp ".;lib/*" src/Controller.class
java -cp ".;lib/*" src/Controller$1.class
None of the above methods worked. They all gave me this error:
Error: Could not find or load main class Controller
What is the problem and how do I do this? It works fine in my IDE, but a company I'm interviewing for wants it with the JAR files included and compilable and runnable via command line. I haven't compiled stuff like that in literally ages, so some help will be greatly appreciated.
Additionally, how can I compile and run the things if they have a deeper folder structure. For example if I have the following folders / paths:
lib/
src/
src/core
src/models
src/models/enums
Your project structure seems to be like this:
root:
lib
src: Controller.java and other files without a package
Correct commands:
javac -sourcepath src -d lib src/*.java
java -cp lib Controller
Explanation:
javac -cp ".;lib/*" *.java
java -cp ".;lib/*" src/Controller
You did not copy any class in lib but your "cp" is set to lib. Also the class Controller has no package so:
java -cp "lib" Controller
I believe you want to be using the javac, and not java for those commands you mention, see below:
javac -cp ".;lib/*" src/Controller.java
Also, what are you using to build the project? If you are using Maven, there are some great plugins that will make your life much easier in terms of packaging a JAR with its dependencies.
Related
I need to be able to compile and run a Java project from the terminal, and without using something like Maven or Gradle. The project structure looks something like this:
project
src
package1
Main.java
package2
Class1.java
Class2.java
bin
lib
jar1.jar
jar2.jar
Main.java contains the main() method, and it needs Class1 and Class2 in order to run. It also needs the .jar files jar1 and jar2, located in the lib folder. I would like all of the .class files to be in the bin folder.
I've tried my hand at figuring out the terminal command in order to compile and run the program but am a bit stuck. This is the best I've managed to figure out:
cd project
javac -d bin/ -cp lib/jar1.jar:lib/jar2.jar src/package2/*.java src/package1/*.java
This successfully compiles, but then when I try to run Main.java:
java -cp lib/jar1.jar:lib/jar2.jar src/package1/Main
I get the following error:
error: invalid flag: src/package1/Main
Usage: javac <options> <source files>
use --help for a list of possible options
Could someone explain to me what I'm doing wrong?
I have compiled my java code using eclipse but not it has to be deployed and a cron job has to execute it. I am trying to execute it from command line in Windows, but getting Could not find or load main class. I tried setting classpath using java -cp bin\com\pega\download\engineclasses but it still throws the same error. My folder structure looks like below
C:\Users\s2517457\G360_Linux\FiddlingPega
|__\bin\com\pega\download\engineclasses\TestUtils.class
|__\src\com\pega\download\engineclasses\TestUtils.java
Please let me know what should be the javac and java commands for this to work.
You should use the following command:
java -cp bin/ com.pega.download.engineclasses.TestUtils
Your are telling to java that the entire bin folder is your classpath and the main class is in the class com.pega.download.engineclasses.TestUtils
If you want to add jars as well, you must call the command like:
java --classpath "bin/;lib/*" com.pega.download.engineclasses.TestUtils
Where lib is the folder containing the Jars files
I currently have a java class which I am attempting to compile using Linux terminal along with a folder (/libs) which contains about 9 .jar folders.
I am struggling trying to attempt this.
I am aware of how to compile a single library to a class:
javac -classpath /libs/example.jar MyMainClass.java
just unsure of how this can apply to multiple libraries.
Thanks.
You can either refer to an entire folder containing all the libraries such as javac -cp lib\* MyMainClass.java
Alternatively, you can refer individual libraries as such: javac -cp lib1\Library.jar;lib2\Library.jar MyMainClass.java (separate with semicolon)
This seems to me to be a trivial question, but I've had a lot of trouble getting an answer.
I've developed a project in eclipse that is dependent on a jar file, which resides in the project's root directory. All my files are in a package "a.b.c" in a src folder. It runs just fine in eclipse. I now want to run this project from the command line. I do this command to compile the project:
javac -classpath dependency.jar -d ./bin/ ./src/a/b/c/*.java
Everything is compiled into class files and put into the bin/a/b/c folder. Then I do these commands to run the project:
cd bin
java -cp ../dependency.jar a.b.c.Main
Now I get "java.lang.NoClassDefFoundError: a/b/c/Main".
So, how do I run a project that is in a package and depends on a jar file?
Just include the current dir in the classpath as well - i.e. java -cp ../dependency.jar:. a.b.c.Main
You also need to specify your compiled files on the classpath, these will contain your a.b.c.Main. On *nix flavor machines the path separator for cp is the colon (:), and on windows it's a semicolon (;), so on *nix, your run command should be (because you're running from the bin directory):
java -cp ../dependency.jar:. a.b.c.Main
I have four java files in my folder. They are all in the same package. Here's the package declaration
package com.osama.GHide
All of these classes are in the same package. I want to know how can I compile them using javac (i mean i do not know how to compile multiple files that are using each other). And once that is done how do I launch then using java command in the CLI? here are the file names.
EnteringPoint.java
HidingProcess.java
ListFiles.java
From the project's root directory:
javac src/com/osama/GHide/*.java
To run, assuming no other dependencies:
java -cp ./src com.osama.GHide.EnteringPoint
(Assuming EnteringPoint has the normal main function.)
The javac command compiles all the .java files in the package's directory. Since they're all in the same package/directory, this works. It also puts the generated .class files in the same directory, which may or may not be what you want.
To put them in a different directory, use the -d option and supply a path.
javac -d bin src/com/osama/GHide/*.java
Then to run:
java -cp ./bin com.osama.GHide.EnteringPoint
You can run by using command :- javac -cp src/com/**/*.java
I am asuming there is no other dependenices and your root class name would be src only.