I tried
javac Something.java -cp ".; some\\directory\\xxx.jar"
and it worked. I could compile the file in this way.
But when running the file, i realized that I must put -cp in front, making it
java -cp ".;some\\directory\\xxx.jar" Something
Why is there such a difference?
Thanks
while running, anything you provide after Something will be treated as program argument
Related
Hello guys i'm new with java and i'm starting to work with packages etc, i have a problem when i try to run my main file located at
C:\Users\Robert\Desktop\Java\latebd\Test.Java
When i run
java -cp C:\Users\Robert\Desktop\Java\latebd\latebd.Test
instead of having the code running i get this (it's in italian bu basically it's telling all the options when i use java -
Why do i get that?
you need to compile it e.g. javac C:\Users\Robert\Desktop\Java\latebd\*
provide the path where it's already compiled as a classpath, and to run it use java -cp "C:\Users\Robert\Desktop\Java\latebd\*" latebd.Test
Try java -cp "C:\Users\Robert\Desktop\Java\latebd\latebd.Test"
Here is the structure of my project:
MyProject
-lib
-mp3agic-0.9.0.jar
-src
-com.company
-Main.java
Inside Main, I import the content of mp3agic:
import com.mpatric.mp3agic.*;
From MyProject, I compile the class using:
javac -cp "lib/*" src/com/company/Main.java
From MyProject/src, I try to run the program with:
java -cp . com/company/Main
and I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mpatric/mp3agic/InvalidDataException
Apparently, the jdk can not find the dependencies from the lib folder when running the class, albeit this problem doesn't occur when compiling.
I guess there's a problem with the syntax of the command, but I just can't find it.
Can you please help me?
EDIT: I solved the problem, moving the lib folder under src and then running:
C:\MyProject\src>javac -cp "lib/*" com/company/Main.java
C:\MyProject\src>java -cp .;"lib/*" com/company/Main
Now, the code compiles and executes without any issue.
HOWEVER, if I leave lib under the project root, this is what happens:
C:\MyProject>javac -cp "lib/*" src/com/company/Main.java
C:\MyProject>java -cp .;"lib/*" src/com/company/Main
Error: Could not find or load main class src.com.company.Main
WHY?
You should use maven or gradle or you'll have to say which console are your using, and which operating system -if you don't know what a console is, you'll first have to google for that before messing with javac directly- But if you insist:
for Linux, OSX, any UNIX-like:
javac -cp ".:lib/mp3agic-0.9.0.jar" src/com/company/Main.java
for Windows:
javac -cp ".;lib/mp3agic-0.9.0.jar" src/com/company/Main.java
To run under windows:
java -cp .;lib\mp4agic-0.9.0.jar com.company.Main
You may need to use slash (/) instead of backslash depending of which console you're using. Or just try "/" or "\" and see what works for you.
If you still get class not found exception, then the issue is not related with classpath or java command but on your code (hard to say, without seeing the code).
java.exe -classpath {path} Main
I think you have to add classpath also and when run the program(not only compile)
I want to compile a file inside a folder with respect to the current path I have opened the command prompt. But I used it like this
javac -d A.java
But this was a possible effort in ubuntu. But it is not working in windows. Can anybody help to make this work in windows7..
You need to give the path, where you want to store your classes with package structure. May be a . for current working directory. Try like this:
javac -d . A.java
My code works fine in Eclipse, no build path errors or anything, however when I try to run my Java program from the command line, I get a classNotFound Exception on one of my inner classes. Not only do I not understand how I am getting this exception, but I am not even sure how to go about debugging it, since it looks and works fine in the IDE environment. Any help is appreciated!
Edit
I am writing a compiler for a subset of Java called J--, so I dont really want to get too into how it all works. But instead of calling javac HelloWorld.java I would call the equivalent j-- HelloWorld.java. javac works fine. You might say well the issue is with your code, but again it compiles and runs fine in Eclipse. So somewhere there seems to be a disconnect. Here is the Windows bash script if it helps:
set BASE_DIR=%~dp0
set j="%BASE_DIR%\..\"
set JAVA=java
set CPATH="%BASE_DIR%\..\lib\j--.jar;%BASE_DIR%\..\lib\spim.jar"
if "%CLASSPATH%" == "" goto runApp
set CPATH=%CPATH%;"%CLASSPATH%"
:runApp
%JAVA% -classpath %CPATH% jminusminus.Main "j--" %*
set JAVA=
set BASE_DIR=
set CPATH=
Edit
Thanks to Aubin, outputting the .jar file and comparing that with the class not found was how I was able to solve this conflict.
Usually Eclipse takes the sources from src and produces classes file into bin.
Try:
java -cp bin a.b.c.d.MyClass
To call your tool as j-- <args> you need to write a shell which embed the command:
java -cp bin a.b.c.d.MyClass $*
I've been having some trouble setting -classpath properly to get a project to run:
What I thought I was supposed to do:
java -classpath /path/to/classes package.Main
The only thing so far that works:
cd /path/to/classes
java -classpath . package.Main
I've also tried to do -classpath /path/to/classes:.
I need to be able to run that class from different directories so I can't use the solution that did work for me.
I am obviously missing something really stupid here.
EDIT: I am using cygwin
The problem was the way classpath is handled in cygwin:
Since java is a windows program it doesn't recognize the cygwin path I gave.
The fix was:
java -classpath `cygpath -wp /path/to/classes/` package.Main
Hope it helps someone else.