0

I have this problem:

To complile and run Java programs I must do the following:

In cmd.exe run comand

path C:\Program Files\Java\jdk1.7.0_75\bin

Then I must complile program:

C:\Documents and Settings\Java\HelloWorld>javac HelloWorld.java

...and run:

C:\Documents and Settings\Java\HelloWorld>java HelloWorld

In order not to follow these steps, I write simple C program:

#include <stdio.h>
#include <stdlib.h>

#define _JAVA
#define JAVAC

int main(int argc, char *argv[])
{
    char comand[261];
    if(argc < 2)
    {
        printf("Error: not enougth arguments. Example: %s <java program name>\n", argv[0]);
        return 0;
    }
    system("path \"C:\\Program Files\\Java\\jdk1.7.0_75\\bin\"");
#ifdef JAVA
    sprintf(comand, "java \"%s\"\0", argv[1]);
#elif defined JAVAC
    sprintf(comand, "javac \"%s\"\0", argv[1]);
#else
#error must defined JAVA or JAVAC
#endif
    system(comand);
    return 0;
}

But it is not work: cmd does not see "java" or "javac".

How can I fix it?

6
  • 1
    Did you add java and javac to your environment variables ? Commented Aug 29, 2015 at 7:54
  • I do this using comand path, not? Commented Aug 29, 2015 at 7:56
  • You can do it with the control panel. Just google it. Commented Aug 29, 2015 at 7:59
  • 4
    I assume that the second system() cannot see the modification of the path variable. See stackoverflow.com/questions/245600/… and try to run both commands in a single system() call. Commented Aug 29, 2015 at 8:03
  • 1
    Each system command is likely to be stand alone. I would use a shell or batch script to do this. Commented Aug 29, 2015 at 8:09

3 Answers 3

0

I think you have to set the path using Control panel because, if you set path using command prompt this will set the path temporarily when command prompt closed the path also destroy so set it permanently using

Control Panel\System and Security\System>Advance system setting > Environment variable > and in system variable add path may this helps you.

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of using a C program, just use a few batch scripts. First is the one to set the path variable jpath.bat. Whenever you install a new SDK, this is all you need to change. You do not need to change any environment variables. This sets it every time. You could have different ones for different SDKs. Java isn't as universal as everyone hopes: sometimes there are version dependencies, especially when it gets to jar files.

@path "%programfiles%\java\jdk1.7.0_75\bin";%path%

Next is a script to do the building jbuild.bat

@setlocal
@call jpath.bat
rem add -cp and any jar files before the %1
javac %1
@endlocal

Next is a script to run the code jrun.bat

@setlocal
@call jpath.bat
rem add -cp and any jar files before %1
java %1
@endlocal

To use

jbuild HelloWorld.java
jrun HelloWorld

Comments

0

For permanent solution of java\jdk1.7.0_75\bin path, it is highly recommended to set an environment path in Control Panel as answered by Abhijit Kumbhar.

If you still don't know what to do, for temporary solution, you should create a new text file in the same folder of your program and edit it with a .bat extension and edit the contents by adding in each line as following...

set path="C:\Program Files\Java\jdk1.7.0_75\bin"
javac programname.java
java programname
pause

You can edit the .bat file with the required programname and run it. This could prevent the tedious job of opening cmd every time and compiling and running the code. Basically, a .bat file does it for you every time you run it.

Edit: If you have already set a permanent path in environment variables, then you only have to use the last 3 lines(except first) in your .bat file. This will still save your time opening a cmd window and typing tedious commands over and over again.

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.