0

I am in c:\new\control folder and want to execute c:\hello.java

I am trying c:\new\control> javac c:\hello.java

Its creating class file but c:\new\control> java hello is giving me Error:could not find o load main class hello

In simple way i wan ask , i have class file in c:\ (say c:\hello.class)
I am in c:\user, how can i execute it???
5
  • This is probably a classpath issue. In which package did you put your hello.java ? Commented Apr 1, 2014 at 14:22
  • its not in any package, its in c:\ Commented Apr 1, 2014 at 14:30
  • @posix: Packages aren't the same as directories. Does your source code have a package statement at the top? Commented Apr 1, 2014 at 14:58
  • @Jon nope its in default package Commented Apr 2, 2014 at 7:23
  • @posix: Then what I've shown should work absolutely fine. You'll need to give more details - including where class files are generated, the error message etc. It would help if you'd show your source, too... I've been assuming that hello.java contains the class declaration for hello... is that the case? Commented Apr 2, 2014 at 7:27

2 Answers 2

3

By default, the .class file will be generated alongside the .java file. Options:

  • Use -d . when compiling to generate the classes relative to the current directory (including creating subdirectories for packages):

    > javac -d . c:\hello.java
    > java hello
    
  • Specify a classpath when running:

    > java c:\hello.java
    > java -cp c:\ hello
    
Sign up to request clarification or add additional context in comments.

4 Comments

not working , may be that work well in package structure, i wan run my class file which is in c:\ from other place like c:\noname\
@posix: Well then the second solution should work. It really doesn't help that you haven't shown any of the code - and "not working" doesn't give us much clue what you can see, either. Please read tinyurl.com/so-list
am tryin to run java prog from another java prog, Process pro = Runtime.getRuntime().exec("javac C:\\Hello.java "); this is working but Process pro = Runtime.getRuntime().exec("java C:\\Hello "); is not working, i tried Process pro = Runtime.getRuntime().exec("cd c:\\ & java C:\\Hello "); but "cd" cmd is not working
@posix: I wish you'd said that before. That's a completely different problem. You should basically use ProcessBuilder and set the working directory instead. cd isn't a binary you can execute; it's part of the command shell. Please learn from this that it's crucial to give context when asking a question.
0

I hope you have created class with different name in your file hello.java. So, try to find out class file with the name of class you mentioned in hello.java file after compilation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.