I have been using IntelliJ to run my java programs that require some external jars. I also learned that if I want to compile and run my program from the command line I should do the following:
java -classpath someJar.jar YourMainClass
or for many libraries:
java -classpath someJar.jar;myJar.jar YourMainClass
However, while placed in the src folder where my class it doesn't seem to find my class.
I also like using the Atom text editor but I don't know any package that can import external libraries like an IDE does. So how do I do it in Atom or in cmd in Windows 10? I am kind of a newbie to java dev outisde my beloved IDE, so I would really appreciate some help.
If you're on a Windows system then try compiling (with the jar) using the following command in cmd:
javac -cp .;jar-name.jar *.java
To run the command with the jar use:
java -cp .;jar-name.jar JavaCodeName
If you're on a Unix system then you can try the following to compile in terminal:
javac -cp jar-name.jar:. *.java
And to run it use:
java -cp jar-name.jar:. JavaCodeName
I'm not too familiar with Atom so I don't know if there is an attachment to do this, but it should work for terminal / command prompt.
Related
I am running Java program from command line. I am referring to only 1 external jar file. i have added entire path to that jar in classpath. even then i get no class def found error while running program in command line. Program compiles without any error.
I think you complied and run the Java program like this
javac -cp fullyqualifiedPathToExternalJar yourfilepath/filename.java
java -cp fullyqualifiedPathToExternalJar yourfilepath/filename
This is totally wrong. When you compiled and run in this manner program compile successfully but not run. This because you have to follow the syntax of java command Properly.
for compiling its Ok.
javac -cp fullyqualifiedPathToExternalJar yourfilepath/filename.java
To run the program you have to add your file path to the classpath:
java -cp fullyqualifiedPathToExternalJar;yourfilepath filename.java //in windows
java -cp fullyqualifiedPathToExternalJar:yourfilepath filename.java //in linux
The syntax is
javac example.java
java example
with folderpath
javac /home/admin/example.java
java -cp /home/admin example//only class name
Might be the chances of jar's compatibility issue. check yous inter dependent jar versions.
I want to run my Java code in remote server with external jar file. I referred to this tutorial and run IntelliJ IDE to generate a jar file of my whole project. Now I can run the following code in local terminal successfully.
javac -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try.java
java -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try
The code will run successfully. However, when I try the same thing in server. The compile process can be finished without any errors or warnings.
javac -cp /Users/natsuapo/out/artifacts/main_jar/main.jar: new_server_try.java
I have checked that the class file new_server_try.class is generated in the directory.
However the second step will return error as Could not find or load main class new_server_try and I am not sure why this happens.
on the second command try giving the full package name .. like shown below
java -cp "/Users/natsuapo/out/artifacts/main_jar/main.jar:lib/*" my.package.MainClass
also with operating system the command differs, check below
Windows
java -cp "Test.jar;lib/*" my.package.MainClass
Unix
java -cp "Test.jar:lib/*" my.package.MainClass
I just installed cygwin and I am wondering how do I compile and run my java code through cygwin?
My java code is at my desktop saved in a file named Java.
Assuming you have Java SDK for Windows installed.
In the simplest case:
Ensure/Add java to PATH in cygwin:
export PATH=$PATH:"/cygdrive/C/Program\ Files/Java/jdk1.8.0_31/bin/"
(don't forget the backslash after Program)
cd to your desktop:
cd /path/to/Desktop
run java compiler:
javac HelloWorld.java
In complex projects you will need to provide a bunch or arguments to javac to make it compile.
Hey I got this problem when running a java program through my cmd.
I use to external libraries : lwjgl.jar and slick-util.jar
when I compile
javac -d bin -sourcepath src -cp ;lib/lwjgl.jar;lib/slick-util.jar src/*.java
everything works fine
but when I try to run it
java -cp bin .;lib/lwjgl.jar;lib/slick-util.jar; Game
I get this error
Error: could not find or load main class .;lib.lwjgl.jar;lib.slick-util.jar;
Replace the space in the run command classpath with a classpath separator
java -cp bin;.;lib/lwjgl.jar;lib/slick-util.jar; Game
^
as an improvement to the run command you could use a classpath wilcards, for example
java -cp ".;bin;lib/*" Game
It is important to use quotes under Windows for this option
I have a folder on my desktop titled "Stuff" and in that folder I have the following:
Hello.java
mail.jar
And Hello.java imports from mail.jar, so I need to tell Hello.java to look for mail.jar.
From a Windows command line and from a unix command line, how can I compile this and run this?
Compile:
javac -cp .;mail.jar Hello.java
where ; is for Windows; use : for *nix.
and run:
java -cp .;mail.jar Hello
where again, use ; for Windows and : for *nix.
-cp tells both javac and java what classpath to use, and as your files are in the local directory where you're executing the command, you can use . for the Hello part and the name of the jar for the paths inside the jar. Wikipedia has a decent article on classpaths.
Mind you, if you're going to be doing this on a regular basis, you may want to set your CLASSPATH environment variable rather than constantly using the -cp flag. Both java and javac use the CLASSPATH variable.
For my own development machine, I actually include . in my CLASSPATH variable, for convenience. It's not something I would do on a production or build/test box, but it's very handy for development purposes. You'd want to have your usual jars in it as well.
Assuming Hello.java does not contain a package declaration, on Windows:
javac -cp mail.jar Hello.java
java -cp mail.jar;. Hello
The only difference on Unix platforms is that you separate the elements of the classpath with a scolon instead of a semicolon:
java -cp mail.jar:. Hello
Follow this tutorial and you should be able to do it in no time:
Java Compilation
You also shouldn't have any problems with the classpath because your classes are in the same folder