There is a Java demo :
package com.demo;
public class Demo {
public static void main(String[] args) {
System.out.println("hello world!");
}
}
How can I use shell to transfer it?
I am not very familiar with Linux. There is a demand:use shell to transfer java method.
Please tell me or give me a demo.
If by transfer you meant run (or execute), then simply do (you have to be in folder where Demo.class, compiled version, is)
java com.demo.Demo
but you have to have java on path, so test that first running
java -version
from console...
Or you can specify absolute path to java program....
You can write the same command into hello.sh if you want, there is not a big difference to have the command in script or execute it from command line.
If your script has to be more general, you should look for Linux scripting: Passing parameters and so on...
Related
suppose this is a simple java program:-
//FileName: Temp.java
class Temp {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
To execute above code we have to command as :-
javac Temp.java // After compilation we get Temp.class(stored in same directory)
java Temp.class
So, I wanted to write a program in NASM(Linux,64 Bit) which gets input file(.java) via command line and store the result of compiler(javac) in RAM itself and execute program via java command by providing memory address to java command.
I found system() gcc function to execute the command line. But, I have no clue how to store javac result in RAM itself and execute java program by providing memory address to java command to execute java program.
Or is there any possible to write such program??
I have a set of instructions to create a Java application that takes multiple arguments when running the application from a CMD line.
The instructions state:
Thus, using the example set of above, assuming the main() method of your program is in a class called JavaClassName, the output should be:
$ java JavaClassName 4 7 file.csv
program output here
My question is:
Isn't this skipping the compile process?
Would they assume that loading the Java classes onto a computer that has never run this application before (or a directory with purely the .java files needed to run); running the cmd
$ java JavaClassName 4 7 file.csv
would output something?
Sidenote: Currently, running that CMD outputs
Error: Could not find or load main class JavaClassName
Have ran through multiple SO questions and online tutorials trying to get this to even run but I have yet to get it to work.
You ask:
Isn't this skipping the compile process?
Absolutely yes. A command line like java JavaClassName 4 7 file.csv assumes that there is a compiled class file "JavaClassName.class" in the current directory (or in some other directory or Zip/Jar file found in the CLASSPATH environment variable). And yes, to produce that "JavaClassName.class" class file, you have to use a java compiler first.
from Java 10 it is possible to run java programs that fit a single file without manually run the compiler first.
It will be compiled on the fly before execution. Nice and useful for scripting.
e.g. writing HelloWorld.java file
public class HelloWorld{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
we can run it as
java HelloWorld.java
We can also add more classes in the single file.
from Java 11 we can also use shebang files
we have to specify which version of the java language you want to use
we have to save the file without .java extension
remember to give executable permissions chmod +x HelloWorld
writing HelloWorld file
#!/path/to/java --source 11
public class HelloWorld{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
and we can run it as
./HelloWorld
Hello stackoverflow community!
I am in the beginning of my journey of becoming a programmer and currently in the process of learning Java. I have strictly been using Eclipse to compile my programs. However, when I try to run the program through the command line I get:
"Error: Could not find or load main class FirstProg."
I've read through some other discussions on the forum and experimented with different methods, but I cannot get it to execute the program.
The path to my program (FirstProg.java) is as follows: C:\Users\smj7v\workspace\LearningJava\src\com\smj\programmingByDoing
When I enter "javac FirstProg.java" in the CMD it compiles the program and I can see the FirstProg.class generated in the path folder, but when I try to execute, "java FirstProg," it throws the error.
I tried doing things like "java com.smj.programmingByDoing.FirstProg" along with other variations but so far nothing has worked. Obviously I am doing something wrong. Please help!
public class FirstProg {
public static void main(String[] args) {
System.out.println("Mr. Mitchell is cool.");
}
}
The program runs fine in Eclipse btw.
here's a sample way of doing
create following class MyTest.java under c:\com\test folder
package com.test;
public class MyTest
{
public static void main(String[] args)
{
System.out.println("test fle");
}
}
now when you are compiling make sure that you use option -d
run following
cd \com\test
javac -d . mytest.java
next from same folder (com\test),
java com.test.MyTest
Step 1: Write Java Program.
Step 2: Compile java file to class file and generate byte code.
Step 3: Byte code translate to machine code and run on JVM.
Steps to write, compile and run java program using command prompt.
(i). Save the program. After using a text editor, such as NotePad, to create your Java program, save the program with a .java extension.
(ii). Open Command Prompt.
(iii). Navigate to the correct folder.
(iv). Set path.
(v). Compile the program.
Example:javac JavaClassName.java
(vi). Run the program.
Example:java JavaClassName
Visit the good blog to read all steps with example and images:
https://javatutorialdetails.blogspot.in/2017/10/how-java-program-work-step-by-step-in.html
Run your class after setting classpath:
set classpath=%classpath%;.;
java com.smj.programmingByDoing.FirstProg
C:\Users\smj7v\workspace\LearningJava\src> javac com\smj\programmingByDoing\FirstProg.java
C:\Users\smj7v\workspace\LearningJava\src> set classpath=%classpath%;.;
C:\Users\smj7v\workspace\LearningJava\src> java com.smj.programmingByDoing.FirstProg
I have executable java apps (as "exe" file) that actually java wrapper executable.
is there a way to grep the java command / parameter from the exe java apps.
Thanks.
In case of a classical main-function you will find the commands in args parameter:
public static void main(String[] args) {
for (String s : args){
System.out.println(s);
}
}
Your question is not so clear, but a I try to answer.
If you are passing parameters from a wrapper exe to a java executable, you oblviosly know which parameters are passing in the exe.
If you need to know passed parameters in java application, instead, you probably have a main matod in this app. This method is declared as follow:
public static void main(String[] args){}
Where args array is exactly what you need: here you find the parameters passed to the java executable. All you have to do is to use this array.
You can connect to the java process using Java Visual VM and inspect some of the properties on the VM, like the system properties, memory settings and more. The tool is available in JDK bin/jvisualvm Visual VM page. You may have to try to connect to the available VMs from the list one by one to find yours. Eclipse appears as but I was able to see JVM args and system properties for it.
I got a strange problem in my windows 7 system running jdk1.6.0_33
When I try to run a simple java program from command prompt, it opens a new window (something like java frame) and suddenly disappears. There is no result shown on command prompt also I am unable to terminate the process (using Ctrl+C) or close command prompt after this. A java process is created each time I do this. I tried to kill process using Task Manager, but that too didn't work.
I am able to run the same program using eclipse.
Here is my program
class HelloWorld{
public static void main(String [] args)
{
System.out.println("Hello World");
}
}
Environment variables are set as follows:
Path=C:\Program Files\Java\jdk1.6.0_33\bin
classpath=.
Commands I used are,
javac HelloWorld.java
java HelloWorld
Why is this happening? Thanks in Advance.
I'm not sure, but I think this is what happens if you use java versus javaw from the command prompt on Windows:
References:
The java command manual page for Windows.
Difference between java/javaw/javaws
If your java class path is configured correctly.
type javac command in command prompt
You will get java compiler information.
After compile the class find your folder location if any .class file is created.