#How do I run a class from a package?

76 messages · Page 1 of 1 (latest)

undone parcel
#

I have 2x .java files, that I eventually want to compile into a single .jar
My plan is for the first .java file to act as a "menu" that then opens the actual program which is the other .java file.

So again, I'm trying to "run" a .java/.class file from inside of a .java file.

I'm a beginner FYI, so I'm not familiar with all of the abbreviations and such.
Tried searching around but cannot find a solution pretty much anywhere. Thanks

split sailBOT
#

Hey, @undone parcel!
Please remember to /close this post once your question has been answered!

opal stratus
undone parcel
#

And how to do that xd

opal stratus
#

As in declare them with the same package, and have them in the same folder.

#

Are you using the command line to compile and run?

undone parcel
#

Using the command line to compile yes

undone parcel
opal stratus
#

You can add

package xyz; // descriptive package name here

On the first line of both files

undone parcel
#

And packages are only for .class files correct?

opal stratus
#

Then you can create a new object from either files in either files again

opal stratus
undone parcel
#

Right..

opal stratus
#

So they are for .class files and .java files too in a way

#

But you write them in your java file

undone parcel
#

Doesn't let me use the package thing tho

opal stratus
#

Compiler will convert it to class file

opal stratus
#

Show please?

undone parcel
#

So the two .java files are in the same folder,

In menu.java I've put "package SC;" at the top
in SC.java I've put "package menu;" at the top

opal stratus
#

Should I send you an example from an earlier project I helped with?

opal stratus
#

That's how javac links them

undone parcel
#

Oh, I thought it was the filenames that was needed

#

so I just make a new name for the "package"?

opal stratus
#

Yeah

undone parcel
#

"The declared package "ChristmasCard" does not match the expected package "" "

undone parcel
#

If you'd like

undone parcel
#

I'm just not really sure how it works, since it doesn't seem to work as you've described

opal stratus
undone parcel
#

Not really sure why the package doesn't work

#

They both have the same name

opal stratus
undone parcel
#

Oh yeah oops, but the file structure is the same on both of them

opal stratus
#

Both have the same error?

#

Do you get the error when you run javac on them?

undone parcel
#

That's the correct one

undone parcel
#

So I run javac on both of them? or only one?

opal stratus
#

Try any one first ig

undone parcel
#

Well still doesn't work

opal stratus
#

Pls show the error

undone parcel
#

But I can't edit the .class file right? So I look inside the other .java file

opal stratus
#

Expected package "" ?

opal stratus
undone parcel
#

Oh I'll try that then

#

Just a bunch of errors and readable text here and there

#

Doesn't really tell me much

#

Oh, I didn't know they had to be under the same folder with the same name as the package

#

Yeah that works now, the package

#

Now I'm not really sure what to do, since it needs to run the class "menu" and then run the class "SC" when a button is pressed

#

But now that these two are packaged together, how would you go about determining which class runs? And which one runs first?

#

Or do I only have a main method in one of the files?

#

How do I run a class from a package?

#

So right now the package works as intended, now I need to figure out how to :

Decide what class runs first
How to run the other class from the first one - since currently I'm able to get it start from the main, but not from the start of the actual class so some of the vars don't init

opal stratus
undone parcel
#

No worries

#

I've basically got it working now, it's just that it's running SC.java from the main method, and not from the actual start of the class

#

Which means some specific variables aren't initalized

opal stratus
#

Are you running java menu?

#

It should be java ChristmasCard/menu if I'm not wrong

undone parcel
#

I'm trying to run menu.java which then runs the class SC

opal stratus
#

Can I request for the code if you're okay?

undone parcel
#

Sure

opal stratus
#

Please do not send the files, they don't show code preview in phone

#

If it's possible, in a message or in a pastebin link. We can also move to DMs if you'd like.

undone parcel
#

Ah

#

Yes I can send a pastebin

#

Send a pastebin to you in DM*s