executing class inside jar file

H

horos11

This is probably a really simple one, but here goes:

I want to run a main method in a class file that is stored inside a
jar file, ie:

java -cp <jre>/demo/jpda/examples.jar com/sun/tools/example/trace/
Trace <my class to trace>

This works if I unjar the associated Trace class, but does *not* work
if I try to run it directly from the jar. I see that java provides a -
jar flag (to run a program from a jar) but that this requires a "Main-
Class" manifest attribute.

Obviously, I'm making it too difficult for myself, so exactly how does
one set up your environment so that you can run a main function from a
class packed up in a jar?

The ultimate output I'd want (which should be reproducible everywhere)
is to run:

java Trace

such that Trace is acquired from a CLASSPATH of

<jdk1.6.0_12>/demo/jpda/examples.jar

Thanks,

Ed
 
M

Mark Space

This works if I unjar the associated Trace class, but does *not* work
if I try to run it directly from the jar. I see that java provides a -
jar flag (to run a program from a jar) but that this requires a "Main-
Class" manifest attribute.


This is working correctly, I think. Only if the Main-Class attribute is
set to Trace will the -jar command run it. Otherwise you can't,
environment doesn't matter.
 
L

Lew

This is probably a really simple one, but here goes:

I want to run a main method in a class file that is stored inside a
jar file, ie [sic]:

java -cp <jre>/demo/jpda/examples.jar com/sun/tools/example/trace/
Trace <my class to trace>

Line wrap is confusing things here.

First of all, Java packages do not contain slashes. Did you mean

java -cp path_to/examples.jar com.sun.tools.example.trace.Trace YourClass
?
Obviously, I'm making it too difficult for myself, so exactly how does
one set up your environment so that you can run a main function from a
class packed up in a jar?

By using the "-cp" (a.k.a., "-classpath") option with a classpath that
includes the JAR, and not using the "-jar" option.

By putting a class name as the argument to the "java" command.
 
A

Arved Sandstrom

This is probably a really simple one, but here goes:

I want to run a main method in a class file that is stored inside a
jar file, ie:

java -cp <jre>/demo/jpda/examples.jar com/sun/tools/example/trace/
Trace <my class to trace>

This works if I unjar the associated Trace class, but does *not* work
if I try to run it directly from the jar. I see that java provides a -
jar flag (to run a program from a jar) but that this requires a "Main-
Class" manifest attribute.

Obviously, I'm making it too difficult for myself, so exactly how does
one set up your environment so that you can run a main function from a
class packed up in a jar?

The ultimate output I'd want (which should be reproducible everywhere)
is to run:

java Trace

such that Trace is acquired from a CLASSPATH of

<jdk1.6.0_12>/demo/jpda/examples.jar

Thanks,

Ed

This does work. If you add the path to a JAR containing your class
(including the jar name as you've shown) to the CLASSPATH, then you can
run with

java MainClassName

(MainClassName qualified with packages as required).

AHS
 
H

horos11

This does work. If you add the path to a JAR containing your class
(including the jar name as you've shown) to the CLASSPATH, then you can
run with

java MainClassName
(MainClassName qualified with packages as required).

hmm... this does work - I can't think of why it didn't when I tried
it, must have been doing something subtly wrong.. thanks.

Anyways, on another topic wrt tracing - is there any way to use the
JDI to print out a trace at a statement level, rather than at the
function level?

Ed
 
L

Lew

(sig elided)

hmm... this does work - I can't think of why it didn't when I tried
it, must have been doing something subtly wrong.. thanks.

Well, the first example you posted used slashes in the class name, which would
make a hash of things.
 

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,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top