Running a .java file from a seperate.java mac - java

Im having trouble with using Desktop.getDesktop().open(). Im trying to run one java file from another one, using this code:
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
public class OpenFile {
public static void main(String[] args){
String program = "HelloWorld.java";
try {
Desktop.getDesktop().open(new File(program));
} catch(IOException e) {
System.out.println(program + " if not on the desktop");
}
}
}
I have a file called HelloWorld.java sitting on my desktop, but I'm getting this error:
Exception in thread "main" java.lang.IllegalArgumentException: The file: HelloWorld.java doesn't exist.
at java.awt.Desktop.checkFileValidation(Desktop.java:210)
at java.awt.Desktop.open(Desktop.java:270)
at OpenFile.main(OpenFile.java:10)
I'm on a Mac, so that may be the problem but I'm not sure. I'd appreciate any advice!

There are two issues that I see here.
You want to "run" a Java program, which is not the same as "opening" a Java file.
Even if you are trying to "open" a Java file using Desktop.getDesktop().open, this does not mean that getDesktop().open points to your "Desktop" folder from where you can open a file on your Mac's Desktop.
To execute your Java program, you need to first compile the Java program using the Java compiler. Then you would be able to "run" your program.
Runtime.getRuntime().exec("javac /Users/username/Desktop/HelloWorld.java");
Runtime.getRuntime().exec("java -classpath /Users/username/Desktop HelloWorld");
The Desktop.getDesktop().open(File file) method simply opens the selected file in the default application for that file type. For example, on a Mac, a text file would be opened by TextEdit.app.
Hope this helps!

Related

Unable to run java code via Command Line. Tried changing CLASSPATH and setting new CLASSPATH via Command Line as well

I am getting below error when I tried to run the included program -
Could not find or load main class ConsoleDemo Caused by:
java.lang.NoClassDefFoundError: inputOutput/ConsoleDemo (wrong name:
ConsoleDemo)
import java.io.Console;
public class ConsoleDemo {
public static void main(String[] args) {
Console cn = System.console();
System.out.print("Enter your name: ");
String name = System.console().readLine(); //cn.readLine() will also work
System.out.print("Enter your password: ");
char[] pass = cn.readPassword();
System.out.println("\n----Details---- \nName: " + name);
System.out.println("Password: " + pass.toString());
}
}
Ok, So after looking into your question. Let's assume your java file is present within Test folder, as shown in below image:
Now, if we need to just compile .java file from command line using command javac ConsoleDemo.java
After compiling, we will have one more file i.e class file for java program.
You can run this program using command java ConsoleDemo, which will execute your java code.
Thanks everyone for the inputs.
After checking I found out that not only this but every java code file was giving the same error.
Everything was running fine in Eclipse but I was unable to run it from cmd.
To rectify it, I just compiled the code in current directory and then ran the .class file using java -cp . {fully qualified name} (from one up directory) or java -cp .. {fully qualified name} (from current directory)
In my case the fully qualified name was inputOutput.ConsoleDemo.
Once again, thanks everyone for your time and inputs

I get an error: An unexpected error occurred while trying to open file hola.jar

i am using Linux ubuntu and i have created a java program named hola.java which is the following program code, this program works perfectly
import javax.swing.*;
import java.awt.*;
public class hola extends JFrame {
JButton b1 = new JButton("presionar");
hola(){
super("Botones");
setSize(250,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flo=new FlowLayout();
setLayout(flo);
add(b1);
setVisible(true);
}
public static void main(String[] args)
{
hola bt = new hola();
}
}
This java program works perfectly when it runs
Now I created a jar file of this program using in command line:
jar cf hola.jar hola.class
This creates a Jar file named hola.jar
I even wrote Main-Class: hola in the manifest.mf file.
When I try to run this by:
java -jar hola.jar
I get an error: An unexpected error occurred while trying to open file hola.jar
Please tell me how to run jar file so that I get an output :'( , what could be the possible reason that i can not run this program as a jar file, even the program works perfectly using "java hola.java"
This error mostly indicates invalid MANIFEST.MF file. Perhaps long line, missing final line terminator, accidental empty line in the middle, ... many things can go wrong. Using -cp just goes around the problem, but does not fix the root cause.
To run a java file within a jar file, you don't need to open it. You simply need to ensure your classpath is having the given jar file
If the class is within a package then you can run using
java -cp hola.jar package.hola
If the class is not in a package then simply use
java -cp hola.jar hola
If you are not within the directory where hola.jar is located, then you can try following:
In case of within package
java -cp /locationOfJar/hola.jar package.hola
or In case of not in package
java -cp /locationOfJar/hola.jar hola
Error can also happen with > 65535 files in the .jar on some OS
I experienced same error message attempting to launch via -jar:
$ java -jar app.jar
Error: An unexpected error occurred while trying to open file: app.jar
Root cause is that my sbt-assembly output .jar file contains more than 65535 entries.
$ zipinfo app.jar |wc -l
65543
Solution: Short term solution was removal of older dependencies to lower the number of assembled .class files below the 16 bit file count limit.
Long term solution will involve testing Zip64 jvm support on the target OS. Unsure of why the zip64 auto negotiation isn't occurring automatically.
This issue is reproducible using sbt-assembly 15.0, openjdk version "11.0.8" on MacOSX 10.15.7.
Start of the code review:
package java.util.zip;
...
public
class ZipOutputStream extends DeflaterOutputStream implements ZipConstants {
/**
* Whether to use ZIP64 for zip files with more than 64k entries.
* Until ZIP64 support in zip implementations is ubiquitous, this
* system property allows the creation of zip files which can be
* read by legacy zip implementations which tolerate "incorrect"
* total entry count fields, such as the ones in jdk6, and even
* some in jdk7.
*/
private static final boolean inhibitZip64 =
Boolean.parseBoolean(
GetPropertyAction.privilegedGetProperty("jdk.util.zip.inhibitZip64"));

Having trouble running Java program with Command Prompt

I'm getting the error Error: Could not find or load main class ExcelFileEditor when trying to run my program. I compiled the program with no errors by:
javac -cp C:\Users\rperera\IdeaProjects\LinkingNames\libs\*;. C:\Users\rperera\IdeaProjects\LinkingNames\src\ExcelFileEditor.java
I tried doing:
java ExcelFileEditor
java -cp C:\Users\rperera\IdeaProjects\LinkingNames\libs\* ExcelFileEditor
but I keep getting the same error. I'd really appreciate it if someone could help me fix this problem!
public static void main(String[] args) {
//this allows the Py4J module in Python to use whichever methods it needs from this class
ExcelFileEditor editor = new ExcelFileEditor(new File(args[0]));
GatewayServer server = new GatewayServer(editor);
server.start();
}
The package is excel.writer
If you are not specifying where to place the generated class files then its going to be in the same directory as the source file.
Try
java -classpath C:\Users\rperera\IdeaProjects\LinkingNames\libs\*;C:\Users\rperera\IdeaProjects\Li‌​nkingNames\src excel.writer.ExcelFileEditor

Java Throws exception in thread main java.lang.NoClassDefFoundError

I am a beginner in Java but not in OOP I have some experience in C and C++ and PHP5
For short I have "hello world" program for test
package com.tutorial.helloworld;
public class helloWorld {
public static void main(String[] args) {
System.out.print("hello world!!!\n");
}
}
When I compile in console with javac compile with no error but when I run Java helloWorld
says
Exception in thread "main" java.lang.NoClassDefFoundError: helloWorld (wrong name:
com/tutorial/helloworld/helloWorld) and much more code
In eclipse run ok. If I delete package statement and compile manually will run ok. but if I keep package statement throws that error.
should I put the class file in a subdirectory com/tutorial/helloworld and is that ok how should I run from terminal and from what directory?
I am on mac os x and I am use to type code in edit and compile and run from console
than run in a ice. I cannot make eclipse to work for c++ (c++ ide)and because of that I try to stick on the console with all languages I know or I learn.
In Java the class name consists of package name + the class's "first name". Therefore write
java com.tutorial.helloworld.helloWorld
You must also know where your .class files are. You must be in the directory containing the com directory for this to work, where the .class file finds itself inside com/tutorial/helloworld directory.

how to launch java file program with processbuilder?

here i'm trying to launch a java program inside another java program.
after some search in the forum i found some clues but my code launches only .exe file and not .java file why?
import java.io.*;
public class Mana
{
public static void main(String args[])
throws IOException, InterruptedException
{
try
{
Process p=Runtime.getRuntime().exec(" D:\\NetBeansProjects\\GetIPAddress\\dist\\GetIPAddress.jar");
}
catch(IOException e1) {System.out.println(e1);}
}
}
for java file you must run javaw.exe(not java, because java.exe shows new console window) from jre:
Process p=Runtime.getRuntime().exec("javaw -jar D:\\NetBeansProjects\\GetIPAddress\\dist\\GetIPAddress.jar");
If you are ok to use 3rd party library, you can use:
http://code.google.com/p/jlibs/wiki/JavaProcessBuilder
Okay, looks like you have several problems.
First, to call a java program as a jar file, you need to have the jar file. Your question first looked like you already had one (producted by Netbeans). So try this:
java -jar D:\NetBeansProjects\GetIPAddress\dist\GetIPAddress.jar
in a console window (cmd.exe on Windows). If this works, then your call as given by Sergey should work too. If it does not work, then you have to look at how to create this jar file.

Categories

Resources