Compiling multiple .jar files with .java class - java

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)

Related

run java program created in intellij from command line

I have a simple Java Maven program that I created in Intellij. It has Two classes Main and Read. I was able to build a jar and run it. However if I zip the source code into a folder , would I be able to compile it using command line? Something like javac ? How should I do this? Shall I run commands in Java folder in the project? Many thanks.
As far as the Java class loader is concerned, a .jar or .zip file is the same as a directory containing the the files, and .jar and .zip files are generally used to distribute compiled Java packages. Here you can read more about it.
For compiling the zip file: use the -classpath option to javac and java. We could, for example:
javac -classpath .:/users/johnr/java:/opt/jdk1.1.6/lib/classes.zip Hello.java

How to compile multiple Java files with Jar dependencies?

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.

java Could not find or load main class from eclipse compiled code

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

Compiling four java files within one package using javac

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.

How to convert a .java file to a .class file?

I am new to Java. I was given some .java files and was told if I could convert them, then I could use them. It is for a game bot I am using. They changed to a new API which I don't understand, so the bot won't run unless it is implemented in .class files.
Can someone explain me how to convert these .java files to fully functional .class files?
You need to compile the .java source code to the .class byte code. Use a Java compiler like javac from Sun's JDK, or if you're using an IDE like Eclipse, it already has a compiler that can generate the .class files for you (usually in the \bin directory).
See also
Wikipedia/Java Development Kit / javac
http://www.eclipse.org/
Related links for javac
Getting Started With Java Tutorial: "Hello World!" / Common Problems (and their solutions!)
The Java Programming Language Compiler - javac
Manual page: Solaris/Windows
You need to compile the java using javac (the java compiler) You can get it as part of the JDK which you can get from Oracle (http://www.oracle.com/technetwork/java/javase/downloads/index.html)
You'll need to run a command like the following
javac foo.java
Which will produce a corresponding foo.class
if you want to compile multiple java files into class files then you follow given procedure.
Syntex:
javac <options> <source files>
Example:
javac -cp -d classfolder *.java
here
options are -cp (copy), -d (directory), classfolder (directory name)
source files are * (all) .java files

Categories

Resources