Java setting classpath in bash - java

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.

Related

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

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!

Running a java program using -cp

so I've checked many posts here and tried everything people suggested and I'm still getting "Could not find or load main class" error, and my professor wasn't very helpful in his answer when I asked for help.
This is what my professor said is supposed to work (he is using OSX).
java -cp classes:lib/json.jar cst420.jsonrpc.client.GroupServerJavaStub http://127.0.0.1:8080
After reading some other posts on this site, I also tried:
java -cp classes:"lib/json.jar" cst420.jsonrpc.client.GroupServerJavaStub http://127.0.0.1:8080
java -cp "lib/json.jar" cst420.jsonrpc.client.GroupServerJavaStub http://127.0.0.1:8080
Still nothing works. I'm in the base directory in a bash prompt ~\GroupJsonRPC and the class file that is in ~\GroupJsonRPC\classes\cst420\jsonrpc\client\GroupServerJavaStub.class exists and is ready for running. The same goes for ~\GroupJsonRPC\lib\json.jar. Any insight into how to get this darn thing running would be greatly appreciated!
PS: I'm using Windows.
The proper command is:
java -cp "classes;lib/json.jar" cst420.jsonrpc.client.GroupServerJavaStub http://127.0.0.1:8080
You need to be sure to include the directory of your class files as well as the jar file(s) with ';' separators, and don't use the -jar option. It invalidates the -cp option.

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.

Compiling and running java application using powershell

I am trying to compile and a sample Helloworld.java file.
I have my jdk installed in C:\Program Files\jdk1.7\bin.
And I have my Helloworld.java in C:\Helloworld.java
I am actually a novice in both powershell and java.
I got some examples from web regarding this but many of them advice to run it like this:
java.exe -classpath $Env:CLASSPATH C:\Helloworld.java
But when I give this in powershell I get an error called 'CLASSPATH' is not defined even after adding it in env variables.
And when I try to compile the code with the following syntax:
$javac C:\Helloworld.java I get an error "javac is not recognised as a token".
So, I am literally lost in this topic . Any step by step procedure to run java programs using powershell for dummies like me will be greatly appreciated.
Setup environment variables in your system.
set JAVA_HOME to C:\Program Files\jdk1.7
add to PATH variable the string %JAVA_HOME%\bin
open new cmd session.
navigate your java source folder.
use javac to compile your java files.
UPDATE:
also if you are experiencing difficulities upon launching an executable via PowerShell check this Microsoft TechNet article
The variables you speak of do not exist in PowerShell as you name them.
The correct variable names are
$Env:JAVA_HOME: C:\jdk1.6.0;
$Env:PATH: C:\jdk1.6.0\bin;.;
$Env:CLASSPATH: C:\jdk1.6.0\lib;.;
As they all must be defined in the ENV: PSDrive
To answer it in a much simpler way , its the path problem .
You probably not have set env variables that's it.
This is how you should set it:
JAVA_HOME: C:\jdk1.6.0;
PATH: C:\jdk1.6.0\bin;.;
CLASSPATH: C:\jdk1.6.0\lib;.;
And later if you open a cmd prompt and type java -version , if you are able to see the java installed version then you are good to go.

Categories