Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #10311 > unrolled thread
| Started by | www <xsli2@yahoo.com> |
|---|---|
| First post | 2011-11-28 13:50 -0800 |
| Last post | 2011-11-29 13:57 -0800 |
| Articles | 10 — 5 participants |
Back to article view | Back to comp.lang.java.programmer
Java classpath question and how to run a program from command line? www <xsli2@yahoo.com> - 2011-11-28 13:50 -0800
Re: Java classpath question and how to run a program from command line? Robert Klemme <shortcutter@googlemail.com> - 2011-11-28 23:08 +0100
Re: Java classpath question and how to run a program from command line? markspace <-@.> - 2011-11-28 14:21 -0800
Re: Java classpath question and how to run a program from command line? Lew <lewbloch@gmail.com> - 2011-11-28 15:58 -0800
Re: Java classpath question and how to run a program from command line? www <xsli2@yahoo.com> - 2011-11-29 06:14 -0800
Re: Java classpath question and how to run a program from command line? www <xsli2@yahoo.com> - 2011-11-29 06:54 -0800
Re: Java classpath question and how to run a program from command line? Lew <lewbloch@gmail.com> - 2011-11-29 07:43 -0800
Re: Java classpath question and how to run a program from command line? www <xsli2@yahoo.com> - 2011-11-29 09:22 -0800
Re: Java classpath question and how to run a program from command line? Lew <lewbloch@gmail.com> - 2011-11-29 12:08 -0800
Re: Java classpath question and how to run a program from command line? Roedy Green <see_website@mindprod.com.invalid> - 2011-11-29 13:57 -0800
| From | www <xsli2@yahoo.com> |
|---|---|
| Date | 2011-11-28 13:50 -0800 |
| Subject | Java classpath question and how to run a program from command line? |
| Message-ID | <85f06dad-9e66-42ef-a232-d98c72832318@cc2g2000vbb.googlegroups.com> |
Hi: Sorry. I am confused about how to run a Java class from command line(I can run it from Eclipse). I use Cygwin on Windows 7. This Java source file is /cygdrive/d/project/src/A/B/C/ MainWindow.java The class file(MainWindow.class) is located at /cygdrive/d/project/bin/ A/BC/MainWindow.class (compiled by Eclipse) MainWindow.java package is: package A.B.C; The following JAR files are also needed to run the program: /cygdrive/ d/project/libs/*.jar In Cygwin command window, "echo $CLASSPATH" does not show anything, so CLASSPATH has not been set. Now in Cygwin command window, I am at /cygdrive/d/project/bin, I typed the command below, but it didn't work: java -classpath /cygdrive/d/project/libs;. A.B.C.MainWindow Can you help me on how to run it from the command line? I also tried to set up CLASSPATH in .bashrc file beforehand: export CLASSPATH=/cygdrive/d/project/libs/;/cygdrive/d/project/bin I am confused: should I separate them by ";" or ":" ? I tried both ways, none of them is working: ". .bashrc" command complains or no complains, but MainWindow still cannot be run. Thank you very much.
[toc] | [next] | [standalone]
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Date | 2011-11-28 23:08 +0100 |
| Message-ID | <9jif3fFmdrU1@mid.individual.net> |
| In reply to | #10311 |
On 11/28/2011 10:50 PM, www wrote: > Hi: > > Sorry. I am confused about how to run a Java class from command line(I > can run it from Eclipse). I use Cygwin on Windows 7. > This Java source file is /cygdrive/d/project/src/A/B/C/ > MainWindow.java > The class file(MainWindow.class) is located at /cygdrive/d/project/bin/ > A/BC/MainWindow.class (compiled by Eclipse) > MainWindow.java package is: > > package A.B.C; > > The following JAR files are also needed to run the program: /cygdrive/ > d/project/libs/*.jar > > In Cygwin command window, "echo $CLASSPATH" does not show anything, so > CLASSPATH has not been set. > > Now in Cygwin command window, I am at /cygdrive/d/project/bin, I typed > the command below, but it didn't work: > > java -classpath /cygdrive/d/project/libs;. A.B.C.MainWindow > > Can you help me on how to run it from the command line? Can you show the error? > I also tried to set up CLASSPATH in .bashrc file beforehand: > export CLASSPATH=/cygdrive/d/project/libs/;/cygdrive/d/project/bin > I am confused: should I separate them by ";" or ":" ? > I tried both ways, none of them is working: ". .bashrc" command > complains or no complains, but MainWindow still cannot be run. If you are starting a Windows JVM chances are that you need to pass Windows paths since /cygdrive cannot be resolved in Windows land. See utility "cygpath". Cheers robert
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2011-11-28 14:21 -0800 |
| Message-ID | <jb11i4$klh$1@dont-email.me> |
| In reply to | #10311 |
On 11/28/2011 1:50 PM, www wrote: > > java -classpath /cygdrive/d/project/libs;. A.B.C.MainWindow > > Can you help me on how to run it from the command line? Besides "show us the error," please also show the directory sturture with the ls command, as well as the working directory (pwd). Something like what's done at the bottom of this page: <http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html>
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2011-11-28 15:58 -0800 |
| Message-ID | <30751211.75.1322524687653.JavaMail.geo-discussion-forums@pruu5> |
| In reply to | #10311 |
www wrote: > Sorry. I am confused about how to run a Java class from command line(I > can run it from Eclipse). I use Cygwin on Windows 7. > This Java source file is /cygdrive/d/project/src/A/B/C/ > MainWindow.java > The class file(MainWindow.class) is located at /cygdrive/d/project/bin/ > A/BC/MainWindow.class (compiled by Eclipse) I assume there's supposed to be a slash between that "B" and "C". > MainWindow.java package is: > > package A.B.C; > > The following JAR files are also needed to run the program: /cygdrive/ > d/project/libs/*.jar > > In Cygwin command window, "echo $CLASSPATH" does not show anything, so > CLASSPATH has not been set. > > Now in Cygwin command window, I am at /cygdrive/d/project/bin, I typed > the command below, but it didn't work: > > java -classpath /cygdrive/d/project/libs;. A.B.C.MainWindow java -classpath 'd:/project/libs/*;." A.B.C.MainWindow You don't get the JARs just by referencing the directory. You need to read the documentation on classpaths. -- Lew > Can you help me on how to run it from the command line? > > I also tried to set up CLASSPATH in .bashrc file beforehand: > export CLASSPATH=/cygdrive/d/project/libs/;/cygdrive/d/project/bin > I am confused: should I separate them by ";" or ":" ? > I tried both ways, none of them is working: ". .bashrc" command > complains or no complains, but MainWindow still cannot be run. > > Thank you very much.
[toc] | [prev] | [next] | [standalone]
| From | www <xsli2@yahoo.com> |
|---|---|
| Date | 2011-11-29 06:14 -0800 |
| Message-ID | <ed80ba1f-864b-4fda-b619-abcd90f6ce2c@gl2g2000vbb.googlegroups.com> |
| In reply to | #10311 |
On Nov 28, 4:50 pm, www <xs...@yahoo.com> wrote:
>
> Now in Cygwin command window, I am at /cygdrive/d/project/bin, I typed
> the command below, but it didn't work:
>
> java -classpath /cygdrive/d/project/libs;. A.B.C.MainWindow
>
The output from the failing command above is showing the following
only:
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-client to select the "client" VM
-server to select the "server" VM
...
[truncated]
Now, I can make it work:
java -classpath "/cygdrive/d/project/libs/*;." A.B.C.MainWindow
by adding the pair of " on the parameter after -classpath AND libs/*,
not libs only. Both corrections are needed to make it work.
Now, I still have another puzzle: I want to set CLASSPATH beforehand,
so I don't need to type "-classpath xxxx". In .bashrc file, I have:
export CLASSPATH="/cygdrive/d/project/libs/*;/cygdrive/d/project/bin"
run ". .bashrc", no complains. If I understand package concept
correctly, now, no matter which directory I am, I can type this
command:
java A.B.C.MainWindow
To my surprise, it says:
Error: Could not find or load main class A.B.C.MainWindow
I don't get it. Since CLASSPATH points to /cygdrive/d/project/bin
already, searching A.B.C.MainWindow should find /cygdrive/d/project/
bin/
A/B/C/MainWindow.class. Why it cannot find it?
Thank you again for all your help.
[toc] | [prev] | [next] | [standalone]
| From | www <xsli2@yahoo.com> |
|---|---|
| Date | 2011-11-29 06:54 -0800 |
| Message-ID | <bb1769d4-8638-4f3c-9778-0f11b0b4956b@m7g2000vbc.googlegroups.com> |
| In reply to | #10324 |
On Nov 29, 9:14 am, www <xs...@yahoo.com> wrote: > > Now, I can make it work: > > java -classpath "/cygdrive/d/project/libs/*;." A.B.C.MainWindow > > by adding the pair of " on the parameter after -classpath AND libs/*, > not libs only. Both corrections are needed to make it work. Sorry, I have to correct the command above. I can only make it work by using relative path, not absolute path. In another words, I need to be at /cygdrive/d/project/bin/ first, then use this command: java -classpath "../libs/*;." A.B.C.MainWindow The following command does not work and I don't understand why: java -classpath "/cygdrive/d/project/libs/*;." A.B.C.MainWindow The error message shows that A.B.C.MainWindow was found and loaded, however, those JAR files were not found and loaded. I have added root "/" to the classpath, and the error is the same: java -classpath "/;/cygdrive/d/project/libs/*;." A.B.C.MainWindow
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2011-11-29 07:43 -0800 |
| Message-ID | <5431520.470.1322581431774.JavaMail.geo-discussion-forums@prij11> |
| In reply to | #10325 |
www wrote:
>> Now, I can make it work:
>>
>> java -classpath "/cygdrive/d/project/libs/*;." A.B.C.MainWindow
>>
>> by adding the pair of " on the parameter after -classpath AND libs/*,
>> not libs only. Both corrections are needed to make it work.
Well, duh.
Except you forgot to switch to Windows nomenclature ("D:/", not "/cygdrive/d/").
You need the quotes because bash sees the semicolon as a statement terminator. Remember? You could also have escaped the semicolon with a backslash instead of using quotes.
You need the asterisk for the reason stated upthread. Did you review the rules for classpath specification as two respondents suggested? It's very basic stuff - the JARs must be called out explicitly, or since recently with a wildcard. You don't pick up JARs just by listing the directory. As stated in the tutorials and documentation. Which you should study.
> Sorry, I have to correct the command above. I can only make it work by
> using relative path, not absolute path. In another words, I need to be
> at /cygdrive/d/project/bin/ first, then use this command:
>
> java -classpath "../libs/*;." A.B.C.MainWindow
>
> The following command does not work and I don't understand why:
>
> java -classpath "/cygdrive/d/project/libs/*;." A.B.C.MainWindow
>
> The error message shows that A.B.C.MainWindow was found and loaded,
> however, those JAR files were not found and loaded. I have added root
> "/" to the classpath, and the error is the same:
>
> java -classpath "/;/cygdrive/d/project/libs/*;." A.B.C.MainWindow
Well, duh. Did you read the answer upthread for that? Someone already explained this to you. You should read the answers that people here generously provide for free at your request.
"java" is a Windows program. It does not recognize Cygwin paths. Again we tell you.
You even got a link upthread for the "java" command and how it takes parameters. You should read the answers that people here generously provide for free at your request. And the Java documentation.
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | www <xsli2@yahoo.com> |
|---|---|
| Date | 2011-11-29 09:22 -0800 |
| Message-ID | <89680c27-0ff8-4c84-b390-6b54ceb5e4cf@cc2g2000vbb.googlegroups.com> |
| In reply to | #10327 |
Thanks. First time I have heard off cygpath concept. I mainly worked
on Linux. Just recently, I started to work on PC and I like to work
inside Cygwin since it has the flavor of Linux, trying to stay away
from Windows nomenclature ("D:/" versus "/cygdrive/d/"). But
apparently, I didn't get away from the issue.
This link shows me the detail about cygpath: http://www.inonit.com/cygwin/faq/
After learning that, there is one more correction in .bashrc file:
using ":" not ";" to separate the two:
export CLASSPATH="/cygdrive/d/project/libs/*:/cygdrive/d/project/bin"
After ". .bashrc", the following command shows the converted *two"
directories in Windows nomenclature, which now is acceptable by Java
command:
"cygpath -wp $CLASSPATH"
Now, no matter which directory I am at, this command will run my
program:
java -classpath `cygpath -wp $CLASSPATH` A.B.C.MainWindow
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2011-11-29 12:08 -0800 |
| Message-ID | <32418883.575.1322597293001.JavaMail.geo-discussion-forums@preu18> |
| In reply to | #10329 |
www wrote: > After learning that, there is one more correction in .bashrc file: > using ":" not ";" to separate the two: > > export CLASSPATH="/cygdrive/d/project/libs/*:/cygdrive/d/project/bin" > Now, no matter which directory I am at, this command will run my > program: > > java -classpath `cygpath -wp $CLASSPATH` A.B.C.MainWindow It's usually a bad idea to use CLASSPATH. Java tries to read it by default, so you already made a mistake by applying cygpath after setting the variable instead of before. export CLASSPATH=`cygpath -wp "/cygdrive/d/project/libs/*:/cygdrive/d/project/bin"` Besides that, using it in conjunction with -cp is silliness. Just use -cp. CLASSPATH is usually too global. -- Lew
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-11-29 13:57 -0800 |
| Message-ID | <67lad7h3iebe7ho8lcvspa0jd8mt9th3g2@4ax.com> |
| In reply to | #10311 |
On Mon, 28 Nov 2011 13:50:14 -0800 (PST), www <xsli2@yahoo.com> wrote, quoted or indirectly quoted someone who said : > >Sorry. I am confused about how to run a Java class from command line(I for help see, in the following order: http://mindprod.com/jgloss/helloworld.html http://mindprod.com/jgloss/javaexe.html http://mindprod.com/jgloss/classpath.html http://mindprod.com/jgloss/package.html http://mindprod.com/jgloss/jar.html http://mindprod.com/jglossjarexe.html http://mindprod.com/jgloss/genjar.html http://mindprod.com/jgloss/ant.html -- Roedy Green Canadian Mind Products http://mindprod.com For me, the appeal of computer progamming is that even though I am quite a klutz, I can still produce something, in a sense perfect, because the computer gives me as many chances as I please to get it right.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web