Re: Running java programs from class files



Nigel Wade wrote:
Knute Johnson wrote:

Babu Kalakrishnan wrote:
Knute Johnson wrote:
Babu Kalakrishnan wrote:

Knute Johnson wrote:

Oliver Wong wrote:

"Knute Johnson" <nospam@xxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:qe%Ig.2$bR.0@xxxxxxxxxxxxxxx

Babu Kalakrishnan wrote:

The commandline for the above example in that case would be :

java -classpath /xyz/abc MyPackage.MyClass

That doesn't work for me although I have seen reference to it before
like that. Could it be that it doesn't work on Windows like that?
It works for me on WinXP SP2:

java -cp "D:\Oliver's Documents\Workspace\Test\bin" D

to run a class called "D" with no package whose classfile is in
"D:\Oliver's Documents\Workspace\Test\bin"

- Oliver
That does for me too. But put it in a package and it won't.

Interesting - Seems to work for me even with classes within a package -

Running TCPServer.class in package test :

java -classpath "C;\Documents and
Settings\Babu\workspace\TestServer\classes" test.TCPServer
Main: Listening for connections on port 2345

Testing on XP Home SP2

BK

package test;

public class Test {
public static void main(String[] args) {
System.out.println("It works!");
}
}

C:\>javac test/Test.java

I'd assume that you now have Test.java and Test.class inside C:\test.

C:\>java test.Test
It works!

OK - here the default classpath assumed by the JVM is "." (which is "C:\"), so it works

C:\>cd test

C:\test>java -cp "C:\test" test.Test
Exception in thread "main" java.lang.NoClassDefFoundError: test/Test

Your commandline here should be :

java -cp "C:\" test.Test

because the classpath is to be set to the root of the package hierarchy - which is C:\ in your case.

With the commandline you used, the class file is expected to be C:\test\test\Test.class


BK
Thank you so much guys, this has confused the s**t out of me for years. I think the most confusing part is the root business.

So now that you solved that one, show me how to use a jar library on the command line when I run a java program. I can make it compile but I can't make it run. I can get it to work if I put the jar file in the Class-Path: line in the manifest and put the jar file in the same directory as the program jar file but not from the command line.


You can't. It is specifically mentioned in the java man page. When running a jar
with the java -jar command syntax, the -cp flag is ignored. The jar file is the
sole definition of the classpath.

Well that's a relief. At least I know now that I'm not going completely nuts.

If your executable jar requires classes from other jars, those jars must be
added to the Class-Path: section of the executable jar's manifest. To further
annoy you the paths in Class-Path: should be relative paths (although absolute
paths do work, at least on Linux). I'm not surprised you're confused, you're
not the only one. I suppose it does offer some measure of security in that a
user of your application can't change the classpath to inject their own jar's
to intercept private information from within the application. Although there's
nothing to stop them unpacking the jar, and re-packaging it with a different
manifest. So what the purpose really is, other than to annoy and confuse,
escapes me.


Thanks very much Nigel.

--

Knute Johnson
email s/nospam/knute/
.



Relevant Pages

  • Re: [SLE] PID file for java process?
    ... "java -jar jarfile.jar" launches the program, ... You can unpack a JAR file using either the "jar" command or the zip, ... interactively or via a shell script. ...
    (SuSE)
  • Re: Java Hello World running problem
    ... a simple HelloWorld java program and compile and run it, ... Below is the HelloWorld program: ... tried to run it by the below command java HelloWorld and got ... That will be a jar file. ...
    (comp.lang.java.programmer)
  • Jar files from command line
    ... I'm trying to run an app from a jar file at command line, ... I keep trying to run command java -jar LottoMadness.jar, ...
    (comp.lang.java.help)
  • Re: How java (command) handles the classname passed
    ... The JVM (started with the java command) searches for the ... in each classpath entry (directory or the root of a jar file). ...
    (comp.lang.java.programmer)
  • Re: How to update a jar file for one source file change?
    ... I'm new to java, so, I may get terminilogy wrong, bear with me. ... we've got a BIG jar file that includes thousands of classes. ... updated java source code was not being used. ...
    (comp.lang.java.programmer)