Saumya Purkayastha. Optional 'thank-you' note:. I have compiled and successfully ran 5 programs as of now. The common thing in all the programs were that the entire program was inside a single class. But now as I have moved to the further chapters I am finding programs with more than 1 class. What do I do in this case?
Should I write all the programs in a single. How do I actually compile these in Command Prompt? Can anyone explain this to me? Christiaan Lombard. You can either put each class in in a separate file or, though this is not considered good programming practice, you can put them all in one file.
When you compile the class containing the main method all other classes the main class needs will also automatically be compiled. Improve this question. Are you having an issue with your approach? Is your build directory on your classpath? Add a comment. Active Oldest Votes. If your files are packaged, then something like this javac com. Improve this answer. Paul Samsotha Paul Samsotha k 33 33 gold badges silver badges bronze badges. My three hours were wasted before I chanced upon the above.
I wrote the different class files in different files and put it all in the same directory and compiled. Compile success. Program runs. Note that the windows classpath separator is a semi-colon ie ; javac -cp. Suppose you have the following Package Named: com. Muhammad Soliman Muhammad Soliman Once you compile your code, you then run this from the top level: java -cp. MyProgram That order thing you describe doesn't matter.
Vidya Vidya So what does the -cp do? I never saw it used with the java command until now, and when I tried it, it worked! Joshua Wilson Joshua Wilson 2, 1 1 gold badge 18 18 silver badges 28 28 bronze badges. McProgramming McProgramming 92 5 5 bronze badges. Ashwant Manikoth Ashwant Manikoth 3 3 silver badges 14 14 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.
Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta.
0コメント