Running java programs from class files

K

Knute Johnson

Andrew said:
Yep. Just wanted to check the internal structure,
specifically the directory sstructure.


OK.. I was wrong! I'll look more closely into the
details you posted.

Andrew T.

No problem.
 
K

Knute Johnson

Andrew said:
Knute Johnson wrote:
...

Aha! I did miss thid command that makes the Jar when I
first read (OK.. skimmed) this, but...

'e'? 'cvfe' as the options, what is 'e'?

I don't see it in the options for either the Win or *nix
version of the jar command.

And, while I'm here, I understand the meaning of 'test/Test.jar'
as the output file, and 'test/*.class' as the input file(s), but what
is 'test.Test' doing in there?

Andrew T.

The e option is the entry point. Same as setting Main-Class in the
manifest file. I'm using the 1.6 beta2 so it may have come with that
set of code. Much simpler than making all those manifest files when you
don't have anything else to put in them.
 
K

Knute Johnson

Nigel said:
Knute said:
Babu said:
Knute Johnson wrote:
Babu Kalakrishnan wrote:

Knute Johnson wrote:

Oliver Wong wrote:


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.
 
A

Andrew Thompson

Knute said:
....
The e option is the entry point. Same as setting Main-Class in the
manifest file.

That's handy.
..I'm using the 1.6 beta2 so it may have come with that
set of code.

I expect so - I am still using the 1.5 SDK.
..Much simpler than making all those manifest files when you
don't have anything else to put in them.

Heck yeah. Especially for dealing with that darn
'last line must be blank' crap that they specify
for the mainfest. I wonder how many hundreds
of man-hours have been wasted due to that alone?

Andrew T.
 
K

Knute Johnson

Babu said:
I don't think it is possible to achieve that if you're using -jar option
of java. Once the -jar option is specified, whatever you specify as a
-classpath or -cp option is essentially ignored, and the effective user
classpath of the JVM becomes the specified jar file and the jars
specified in that jar's manifest.

An alternative would be to not use the -jar option, but to run it as :

java -cp /path_to_lib_jar/lib.jar:/path_to_test_jar/test.jar test.Test

(i.e by explicitly specifying the main class to invoke, and specifying
all the jar files in the classpath option)

BK

I did try that and it does work just fine. I think you are correct that
you can't do what I was trying to do. See Nigel's post below.

Thanks very much Babu.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,786
Messages
2,569,626
Members
45,328
Latest member
66Teonna9

Latest Threads

Top