LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Running a Java executable class from another executable class (https://www.linuxquestions.org/questions/programming-9/running-a-java-executable-class-from-another-executable-class-343831/)

LUB997 07-16-2005 04:48 PM

Running a Java executable class from another executable class
 
I am programming with Java and have 2 executable classes, win.class and programManager.class. Both have main() methods and are executable. I want to execute programManager.class from win.class. Can this be done, and if so, what command would I use to do it in my win.java file? All .java and .class files are in the same directory.

Looking_Lost 07-16-2005 06:23 PM

Have a look here:

Classloader

You'll want to use

ClassLoader.getSystemClassLoader();

rather than the ClassLoader example they use.

Pretty interesting stuff.

jlliagre 07-17-2005 02:03 AM

You can also simply call the other class main method from the first one.
Code:

public class win
{
  ...
  programManager.main(args);
  ...
}


mohama 07-17-2005 06:52 AM

answer
 
hello LUB997 ...
i just wanted to ask if u know what is Object-Oriented Programming ??

i don't realy know what your program supposed to do , but u could change your design and insted of using the static main method , u just could create object of that specific class and send it messeges so that it will respond the way u like ... .. .((( modularity is important thing for programmer )))

modularity is : ---->>
A quality of a system where it consists of various parts which separate cleanly and fit together well. High modularity costs some design time but pays back well through clarity, elegance, maintainability and -->> FLEXIBILITY <<-- .

good luck , :-)

mohama 07-17-2005 07:08 AM

another small thing ... .. .
 
Quote:

public class win
{
...
programManager.main(args);
...
}
i just wanted to say that the piece of code above wont compile ... ( identifier expected error , ))) i quess ((( )
it should be like this ... .. .

Code:

public class win
{
  ...
    public static void main ( String argv[] )
    {
        ...
        programManager.main(args);
        ...
    }
  ...
}

thats it ,
good luck , :-)

jlliagre 07-17-2005 10:14 AM

Hi mohama,

I tried your example, but it compiles neither, here's what I got:
Code:

win.java:3: illegal start of type
        ...
        ^
win.java:7: <identifier> expected
        }
        ^
2 errors

Hmm, do you really know object oriented programming ???

LUB997 07-17-2005 10:33 PM

I got it to work a different way. I finally figured out how to apply the example in my Java book to my own program and ended up using a package. I just put "package win.manager" at the top of my source file for programManager.java and then on win.java did "import win.manager" and then to call my programManager method from my programManager.class stored in win.manager I just did "programManager();" in my win.java source file. Worked beautifully, but it took me a while to figure out since I don't know a whole lot about Java or object oriented programming yet. I've taken a year of C# in school, but we still haven't really gotten into the object oriented aspects of it yet. For those wondering what kind of program I am making, I am making a Windows for Workgroups 3.11 clone in Java. Not for any particular purpose, but mainly just for the sake of doing something complicated to advance my programming skills, but not so complicated that I can't do it. It of course will not be able to run Windows 3.1 programs, but will just be the Windows 3.1 GUI. win.java is my main screen (the background), and programManager.java is just that; the program manager. As anybody who remembers the DOS days recalls, you used to type "win" from the command prompt to start Windows 3.1. With mine you type "java win" from the bash prompt. If anybody's interested in it, I'll gladly share the sourcecode on it when it's finished, but not until then because the idea here is for me to learn something after all, and not just have others do it for me. Once I succeed at creating it in Java I will then try something more complicated and Linux oriented such as C and maybe GTK or QT for the graphics. My real goal is to create Linux programs, and thus I am really more interested in C than Java, but in this case I chose Java because you can do a lot with it fairly easily, and this is my first run with a program that has a detailed GUI, so I didn't want to use a language that would complicate things too much for me. Thanks for all the responses!

mohama 07-18-2005 04:45 AM

hi jlliagre .. .
 
Quote:

win.java:3: illegal start of type
... <<------------------- what such a fool error , :-o :- ( :- o :~ o :~|
^
win.java:7: <identifier> expected
}
^
2 errors <<------------ not realy two errors , the second is a result of the first ... .. . lol
ofcourse it will not compile since u repeat the same errors from that piece of code , there is nothing called ( ... ) in java so you should replace it with java type like ( ArrayList , AvlTree , Graph , .... ) or a type that u made in the same folder or in an imported package . ( i realy wonder how u make such an errors ???!!! )

an other thing , there is nothing for this to do with object oriented programming ,
Quote:

Hmm, do you really know object oriented programming ???
yes i do ... in OOP course i get 95 last semester ... .. . :-)

good luck .... LUB997
bye,

jlliagre 07-18-2005 12:29 PM

Mohama, please think twice before posting, read again this thread and see that you started with this inappropriate comment:

Quote:

i just wanted to say that the piece of code above wont compile ... ( identifier expected error , ))) i quess ((( )
it should be like this ... .. .
So to be sure you understand:

I used an ellipsis (three dots) in my sample code to represent parts unrelated with the demonstration,
"programManager.main(args);" can be used from anywhere inside the win class.

The sample code you posted is not adding anything, but on the opposite seems to limit the main call from the local main, which is wrong.

I was hoping you take my previous posting with humour, but am disappointed and sorry you missed it ...

mohama 07-19-2005 03:55 AM

hellooooo
 
Quote:

I used an ellipsis (three dots) in my sample code to represent parts unrelated with the demonstration,"programManager.main(args);" can be used from anywhere inside the win class.
the underlined is not true , since u can't use it from any where inside the win class ( exactly the same error which u made in that piece of code ) it should be used from a body of one or more of the win class methods including the main method ,

Quote:

I was hoping you take my previous posting with humour, but am disappointed and sorry you missed it ...
well , no ...you shouldn't be niether sorry nor disappointed cause i didn't miss it , further more i wish u didn't and will not miss the humour in mine ...

bye ;~))

jlliagre 07-19-2005 12:34 PM

Quote:

it should be used from a body of one or more of the win class methods including the main method
This is just wrong ... it can be used from outside class methods too.

mohama 07-20-2005 04:22 AM

ahaaa
 
and how is that ???
inner class ???

if yes then this thread become with no mean , the perpuse of this forum is to help others and not ... .. .
so ...

bye ,

elyk1212 07-20-2005 02:03 PM

Hi,
Can you state the purpose of the program and what it will be doing. In Software Engineering/and Hardware Engineering, we are taught that design is the most important step to coding/or simulations. A universal truth. Please let me know and you *may* rework the design if needed.
If you are invoking main within a main, it may be time to look at design, as that is not a very common case (never seen a need for that one in all my years programming, but there *could* be, I guess. This all depends on the design).

elyk1212 07-20-2005 02:04 PM

And,.... Yes.. I would use an object oriented approach as suggested. Java lends itself to this nicely. But, all depends on design requirements.

jlliagre 07-20-2005 02:26 PM

Quote:

ahaaa ( post #12)

and how is that ???
I'll tell you when you stop claiming my perfectly right solution is not :D
Quote:

inner class ???
nope
Quote:

if yes then this thread become with no mean , the perpuse of this forum is to help others and not ... .. .
How are you helpful when writing:
Quote:

i just wanted to say that the piece of code above wont compile
while the code was intentionally not meant to be compiled but just to show how to call a class main method from elsewhere ?


All times are GMT -5. The time now is 12:56 PM.