Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Java from Cygwin

I'm trying to write a BASH script to get my Java program to run(common issue, right?). I just can't quite get it to work. After many edits, here's how I am trying to set the classpath and then execute the program:

java -classpath 'cygpath -u "/cygdrive/c/Projects/common/lib/rome-1.0.jar:/cygdrive
/c/Projects/common/lib/jdom-1.0.jar:/cygdrive/c/Projects/common/lib/jsoup-1.6.1.jar:
/cygdrive/c/Projects/common/lib/mysql-connector-java-5.1.18-bin.jar:/cygdrive/c/Projects
/Freereader/bin/"' com.free.syndication.SQLfeeder

Sorry the the jumble, I'm just trying to do everything at once. It tells me that the main class of my program cannot be found!((

Any ideas?

like image 925
blaughli Avatar asked Mar 13 '12 19:03

blaughli


People also ask

Does Cygwin use Java?

Java, version 1.6. 0_04 or later. Because there's no Java for Cygwin, the toolkit will use whatever native Win32 Java it can find. GNU gcc, make, buildutils.

How do I run a Java file in Terminal?

Type 'javac MyFirstJavaProgram.java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set). Now, type ' java MyFirstJavaProgram ' to run your program. You will be able to see the result printed on the window.


2 Answers

  • Java classpath uses semicolon as the token separator.
  • Use backticks instead of single quotes

Try:

java -classpath `cygpath -u "/cygdrive/c/Projects/common/lib/rome-1.0.jar;/cygdrive
/c/Projects/common/lib/jdom-1.0.jar;/cygdrive/c/Projects/common/lib/jsoup-1.6.1.jar;
/cygdrive/c/Projects/common/lib/mysql-connector-java-5.1.18-bin.jar;/cygdrive/c/Projects
/Freereader/bin/"` com.free.syndication.SQLfeeder
like image 60
rrhartjr Avatar answered Sep 22 '22 19:09

rrhartjr


  • In bash, the syntax $(command) is clearer than the backticks `command`
  • cygpath has a -p option to convert PATH-like values (as opposed to single path names) between Windows and Unix, i.e.
    • cygpath -pu 'C:\Users\me\bin;C:\Users\me\project\bin' will give /cygdrive/c/Users/me/bin:/cygdrive/c/Users/me/project/bin
    • cygpath -pw will do the same in the opposite direction

Note that cygpath -u "/cygdrive/c" (as in your question) will not change anything, since the directory name is already in the desired (Unix) syntax. You could omit it just as well.

So, the command becomes:

CP="C:/Projects/common/lib/rome-1.0.jar;C:/Projects/common/lib/jdom-1.0.jar;C:/Projects/common/lib/jsoup-1.6.1.jar;
C:/Projects/common/lib/mysql-connector-java-5.1.18-bin.jar;C:/Projects
/Freereader/bin"
# for a Windows Java binary:
java -classpath "$(cygpath -pw "$CP")" com.free.syndication.SQLfeeder 
# for a Unix Java binary:
java -classpath "$(cygpath -pu "$CP")" com.free.syndication.SQLfeeder 

Alternatively, you can start with a Unix-style class path, but the commands stay the same. In either case, you can of course omit the call to cygpath if the class path is already in the desired syntax.

like image 34
user1010997 Avatar answered Sep 19 '22 19:09

user1010997