I am new to Java (as of today!) and am trying to run a very simple program in terminal. Normally, when I run a python (still pretty new) program in Terminal I would simple type in "python name.py" in to terminal and it would run. When I type "Java name.java" it does absolutely nothing.
I opened TextWrangler and selected "Run in Terminal" and it returned this error:
"This file doesn't appear to contain a valid 'shebang' line (application error code: 13304)"
My program is named "hello.java" and it contains the code below.
What am I doing wrong?
System.out.println("Hello!");
Open the terminal, go to the directory where the file is located and do this:
javac -classpath . name.java // compile the code
java name // run the program
Of course, both javac and java must be available in the operating system's PATH variable, but in a Mac that's already configured.
Type
javac helloworld.java
java helloworld
The first line calls the compiler and compiles it in the current directory
then the next line runs it
"Run in terminal" attempts to run the software as an interpreted script. On UNIX-like systems (meaning almost everything except Windows), a shebang line is used to indicate, what interpreter is to be used. It consists of the characters #! followed by the command to invoke the interpreter (e.g. #!/usr/bin/python or #!/usr/bin/ruby). However, Java is not (only) interpreted.
java only runs compiled Java bytecode, so it does not work on source files
Instead, head over to the terminal yourself, compile the code with javac and run the resulting bytecode (the .class files) with java:
javac the_file_name.java
java the_class_name
where the_file_name.java is the file containing main(...) and the_class_name is the name of the class containing main(...) (this should usually be the same as the_file_name)
Related
I am having trouble in setting up Sublime Text for Java programming.
When using the JavaC Build System, I am getting an error like this in the terminal:
bash: javac: command not found
[Finished in 0.1s with exit code 127]
[shell_cmd: javac “/home/vinays/Documents/HelloWorld.java”]
[dir: /home/vinays/Documents]
[path: /app/utils/bin:/app/sublime_merge/bin:/app/bin:/usr/bin]
Then I had made myself a new build system like this:
}
shell_cmd”: “java $file_name”,
“working_dir”: “${project_path:${folder}}”,
“path”: “/usr/bin/java”
}
Again it went wrong and shows:
/usr/bin/env: ‘bash’: No such file or directory
[Finished in 0.0s with exit code 127]
[shell_cmd: java HelloWorld.java]
[dir: /home/vinays/Documents]
[path: /app/utils/bin:/app/sublime_merge/bin:/app/bin:/usr/bin]
You've got a lot going wrong here, so lets take it in stages.
“bash: javac: command not found
[Finished in 0.1s with exit code 127]
[shell_cmd: javac “/home/vinays/Documents/HelloWorld.java”]
[dir: /home/vinays/Documents]
[path: /app/utils/bin:/app/sublime_merge/bin:/app/bin:/usr/bin]”
This is an indication that the sublime-build file you tried to use (perhaps the one that ships with Sublime) tried to invoke javac, but it was not found anywhere on the path.
You also included the following in your answer:
~$ which java
/usr/bin/java
This is an indication that java is indeed available on the path; this is the command that you use to execute a Java program.
Since you have java but do not seem to have javac, my guess would be that you installed a JRE (Java Runtime Environment) and not a JDK (Java Development Kit). The first is meant for running Java applications only, while the second actually contains the tools and additional files needed to compile Java code.
So, your best bet is to verify that you actually have a JDK installed (perhaps by executing which javac to see what it says).
Further to that, you also provided this sublime-build file:
{
"shell_cmd": "java $file_name",
"working_dir": "${project_path:${folder}}",
"path": "/usr/bin/java"
}
This is wrong for a couple of reasons. First, it's using java and not javac, so even if it did work and launch java, it would fail because java is for executing the compiled class file, not for compiling your code.
Secondly, path in a sublime-build file does not do what you think it does. Specifically, it tells Sublime to completely erase the contents of your PATH environment variable and then replace it with the content that you provide.
The PATH is for specifying the list of directories in which programs you want to run interactively live; here you've set it to the actual physical location of the java program itself (i.e. not a folder).
With that change in place, nothing can run any longer because the PATH is entirely invalidated. Hence your error here:
/usr/bin/env: ‘bash’: No such file or directory
[Finished in 0.0s with exit code 127]
[shell_cmd: java HelloWorld.java]
[dir: /home/vinays/Documents]
[path: /app/utils/bin:/app/sublime_merge/bin:/app/bin:/usr/bin]
This is the OS telling you that Sublime tried to run bash (the default shell), but it could not be found. It can't be found because the path was broken by your build (the path: listed is gathered for the debug diagnostic before the build starts, so it represents what the path was prior to the sublime-build file breaking it).
It's also worth noting that a command like javac Something.java just compiles the Java code, and then stops; this is almost certainly not what you want to do. You probably want to actually execute the code as well.
So, after you ensure that you actually have both java and javac, you should be able to get yourself up and running with a sublime-build file like this:
{
"shell_cmd": "javac \"$file_name\" && java \"$file_base_name\"",
"working_dir": "${project_path:${folder}}",
"selector": "source.java"
}
This will compile the single Java file and then, if it worked, execute it. Note that because your other build broke the PATH, you need to restart Sublime before any build will work.
This also doesn't help you work with any Java program that's more complicated than a single file. Java as a language does not lend itself well to ad-hoc compiling and running of files because the class files need to be in the appropriate package-specific location.
At some point you will need to upgrade yourself to using some Java-aware external build tool from Sublime, such as Ant or the like.
Yes Sir I have verified my system commands i had got the following result:
~$ which java
/usr/bin/java
~$ which javac
/usr/bin/javac
More Over My path in sublime text is also Like This:
{
"shell_cmd": "java $file_name",
"working_dir": "${project_path:${folder}}",
"path": "/usr/bin/java"
}
I am doing an assignment for class and I was given a jar file for the Display board and I have to code some basic functions for it in a separate Java file. Now, I need the Display to be called in the program I'm writing, and so I used:
(I'm using a window PC)
javac -cp lab1.jar;.Wrapping.java
However, every time I try this, it gives me this error:
javac: no source files
usage: javac <options><source files>
use -help...
Now, I've changed my directory to the proper folder and the file is in there, and still, I cannot get it to run. What am I doing wrong? I can get other programs to compile and run from the command prompt, it is just this one that I cannot. Any help?
If your command it posted as you ran it literally, without the space between . and Wrapping.java, you need a space there.
Try this command to compile:
set classpath=lab1.jar
javac Wrapping.java
OR
javac -cp lab1.jar Wrapping.java
Try this command to execute:
set classpath=lab1.jar;.
java Wrapping ::Make sure its class name with main()
OR
java -cp lab1.jar;. Wrapping
The problem I'm currently having is that I am trying to execute a java file using command prompt, I understand the PATH being set to the jdk's file. Though my java file contains libraries and has to import the libraries, how would I ' import the libraries ' when it runs?
Sample command :
javac ClassName.java 1 1 1
When it executes it errors on the imports so what should I do?
The .jar files are Java "libraries". What you need is something like:
> javac ClassName.java
> java -cp Library1.jar:Library2.jar ClassName.class
The first line (javac) compiles the Java code into a class file. The second line runs the compiled class. The '-cp' option sets the CLASSPATH (makes the code in the jar files available at runtime). Note: the exact syntax will depend on if you are using Mac OSX/Linux or Windows. Windows uses the ';' character to separate the jar file names.
Several issues here:
Are you compiling or executing? javac is the compiler. java is the runtime virtual machine.
ClassName.java is source. ClassName.class is the compiled bytecode run by java.
As a general rule, your environment should contain the variable JAVA_HOME, and that should point at the directory where the JDK resides on your computer.
I have a Main.java file and I want to run the program passing it test.txt
I know in command line I can write javac Main.java
After compiling I can write java Main test.txt and this will accomplish running the file and passing test.txt
If I wanted instead to be able to just write main test.txt and have that trigger my Main.class file to run is that possible and if so how?
(Edit: Based on your comment, let me expand to add a couple more situations)
If your goal is to have someone else run your program who does not have Java installed, and you do not wish to have them install a Java runtime environment before running your app, what you need is a program that converts the .class or .jar files into a native executable for the platform you are using. How to do this has been covered in other questions, eg: Compiling a java program into an executable . Essentially, you use a program like JCG (GNU Compiler for Java) or Excelsior JET (a commercial product) to expand the byte code into full native code with a mini-JRE built in.
If your goal is to save typing, there are a number of strategies. Others have suggested alias commands, which work well on linux.
A slightly more portable option that you could ship with your program would be a shell script. Granted, shell scripts only run on linux or other OS's with shell script interpreters installed.
Here is an example shell script. You paste this into a text editor and save it as main with no extensio. The $1 passes the parameter argument fyi.
#!/bin/sh
java Main $1
presuming you name your shell script just "main" with no extension, you could call main test.txt to execute your program now.
If you are on Windows, you might want to create a windows shortcut, and point the shortcut to "java Main test.text", using the full paths if necessary (if the paths are not already set). Of course, this does not make the parameter easy to change every time you run it, you would have to edit the shortcut.
add an alias
e.g. under a mac edit your .bash_profile with the following line
alias main='java main'
don't forget to open a new console to see your alias working
Depends on your operating system. On Linux with the bash shell, for instance, you can set up an alias to expand your main into java -cp myjar.jar main.
Linux can also be configured to 'understand' Java class flies as a binary format directly see here (linux kernel documentation).
If you're on windows, you'll have to wait for answer from someone with more knowledge about that than I.
Good luck!
I am on Arch Linux, I just installed JRE and JDK and all the proper bin files (javac and java) are in /opt/java/bin/
I simply compiled a standard hello world, and compiled it with javac running javac ./hello.java and that made a class.
Now my problem is running it. I run java ./helloworld.class and it gives me an error, even if the file I point java to is non-existant:
Exception in thread "main" java.lang.NoClassDefFoundError: //helloworld/class
Caused by: java.lang.ClassNotFoundException: ..helloworld.class
(..omitted for clarity..)
Could not find the main class: ./helloworld.class. Program will exit.
You will notice the first line of the error, it munges the path //helloworld/class
When I feed java an absolute path, i.e java /home/foo/helloworld.class it gives the same error, but replaces the path's / with . in the first line, again munged.
What do you think is wrong? I really don't know why it is doing this..
When you run java, you just pass it the fully qualified class name (including package), not the file name.
java helloworld will look for helloworld.class.
java helloworld.class will look for helloworld/class.class
You do not run a file as
# java file.class
you run it as
# javac PATH/file.java
# java PATH/file
Do not add .class while using JAVA command.
Actually you should compile it like this
javac helloword.java
run the program
java helloword
And yet another thing: add command line option "-classpath ." or it short version "-cp .", i.e. your command line should look like:
java -cp . helloworld
this is if your class is in your current directory. Otherwise "." should be replaced by path where the class(es) may be found.