Bash on windows can't find class even after setting classpath - java

I need to include .jar file when running the java class. When I use cmd on my windows, I can successfully run using the following command
java -cp .;MyLib.jar; MyClass
However when I tried to use bash on the same PC, I got error - can't find or load main class. I searched online and some say I needed to use ./* instead of just . for the current directory. So far I tried these commands:
java -cp .:MyLib.jar: MyClass
java -cp ./*:MyLib.jar: MyClass
java -cp ".:MyLib.jar:" MyClass
java -cp "./*:MyLib.jar:" MyClass
only getting "ClassNotFoundException" every time.I know the class exists because if I switch to cmd when it runs fine. I was also able to run the class on bash shell on a mac using the following command
java -cp .:MyLib.jar: MyClass
Then I built a simple class and ran in bash without classpath
java MyTest
So using -classpath caused the issue. How do I include the current directory in bash? Why did it work on bash on a mac? Is it because I was running bash on windows? I am so puzzled by this problem.
And if it helps, I can compile in bash just not able to run the class. javac -cp MyLib.jar MyClass.java compiled the file in bash. I am working under the same directory where MyClass.class is.
Edit: the solution is java -cp ".;MyLib.jar;" MyClass. I thought I posted here for anyone who ran into the same issue as I did. Please see comments for explanation. Thank you torek!

Related

Class not found when running program from command line

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)

How to import external jars in command line or Atom?

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.

How to run java code from shell script on linux

I want to set a cron job for my java code and I have tried this from project directory
java -classpath .:/home/project/lib/* pkg_name.my_code
and it works fine, however I dont know how to run it from any other directory[like in script]
I have tried to add diroctry (having compiled classes) in classpath like this
java -classpath .:/home/project/lib/*;/home/project/pkg_name/* pkg_name.my_code
also tried
java -classpath ".:/home/project/lib/*;/home/project/pkg_name/*" pkg_name.my_code
but it gives error:
**Error: Could not find or load main class pkg_name.my_code
**
can any please help me ?
If you want to run your project from another directory, then you need to include your project in classpath. So you can do this
java -classpath ".:/home/project/lib/*:/home/project" pkg_name.my_code
For example :
java -classpath ".:/home/test/runjavafromanotherdirectory/lib/*:./runjavafromanotherdirectory" com.test.Main
One of your mistake is you are using ; instead of :.

Antlr: Could not find or local main class even when it is there

I have an antlr grammar and I have written and listener for it. I am running the latest version of ubuntu.
I run the following commands
#Generate
java -jar antlr-4.1-complete.jar -package pascal -o pascal pascal.g4
#Compile
javac -cp antlr-4.1-complete.jar pascal/*.java
#Run
java -cp antlr-4.1-complete.jar pascal.pascal sample1.pascal
Everything seems to go fine until the final run command. I get this : https://www.dropbox.com/sh/8majts6voe7c8v9/AADf5GmB3vpbURrp4ONlqSW-a
When I try to do the Run command I get "Error: Could not find or load main class pascal.pascal
I have tried to change pascal.pascal to pascal/pascal.
I have Java "1.7.0_55" installed.
I really do not understand why this does not work as the files with the same commands run on my friends mac.
I do not need help with the actual work, just as to why I keep getting this Error.
If you have any ideas it would be appreciated
You didn't add the pascal package to the classpath.
From Mac OS X and Linux do:
java -cp .:antlr-4.4-complete.jar pascal.pascal sample1.pascal
and from Windows:
java -cp .;antlr-4.4-complete.jar pascal.pascal sample1.pascal
Also have a look at Java's code conventions: class names should start with an upper case.

Cannot find class in classpath

I've been reading up on this error but am now confused. I'm trying to run my java test file from the win7 cmd line but am getting that dreaded error cannot find class in classpath.
The script runs fine as a testNG file inside the Eclipse IDE but not from the cmd line.
My classpath dump:
C:\lib\selenium-java-2.37.0.jar;
C:\lib\selenium-server-standalone-2.37.0.jar;
C:\EclipseIDE\ProteinBar\bin;
C:\EclipseIDE\ProteinBar\src\com\proteinbar\staging;
C:\TestNG;
My cmd line test string:
java -cp C:\lib\*;C:\EclipseIDE\ProteinBar\bin org.testng.TestNG DeleteBillingAddress.xml
Thanks for any help...
it doesn't appear that the testng jars are on your classpath. I'm guessing they live under c:/testng so you'll want to add something to your -cp reflecting that.
You need to specify the jars individually, e.g.,
java -cp C:\lib\selenium-java-2.37.0.jar;C:\lib\selenium-server-standalone-2.37.0.jar;...

Categories