java.lang.runtime.exec cannot resolve symbol

D

Darren

i'm invoking to use Java's 'exec' but the documentation says its in the
java.lang.runtime class.. i import it as well as java.lang.process but the
when i compile the programme that calls it i get a "cannot resolve symbol"
on the "exec" line. which is 'exec(JarSignerPath);'

The code for this class alone is below;

package uk.me.g7wap.jarmaker;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionEvent;

import java.io.File;
import java.lang.Runtime;
import java.lang.Process;
public class Jarmaker
{

private static String JarSignerPath=null;

public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try
{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel
");

}
catch (Exception ex)
{
System.out.println("Failed loading L&F: ");
ex.printStackTrace();
}
final JFrame w = new JFrame("Desktop Window");
final JDesktopPane desktop = new JDesktopPane();
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File");
JMenuItem menuItem = new JMenuItem("New");
JMenuItem menuFileNewJar = new JMenuItem("Create JAR",KeyEvent.VK_J);
JMenuItem menuFileNewZip = new JMenuItem ("Create ZIP",KeyEvent.VK_Z);
JMenuItem menuFileOpenJar = new JMenuItem("Open JAR",KeyEvent.VK_F3);
JMenuItem menuFileOpenZip = new JMenuItem ("Open ZIP",KeyEvent.VK_F3);
JMenuItem menuFileSetRoot = new JMenuItem("Set Root
Dir",KeyEvent.VK_R);
JMenuItem menuFileSignJar =new JMenuItem("Sign Jar",KeyEvent.VK_J);
JMenuItem menuFileExit = new JMenuItem("Exit",KeyEvent.VK_X);
w.setContentPane(desktop);
w.setJMenuBar(menuBar);
menuBar.add(menuFile);
menuFile.add(menuItem);

menuFileNewJar.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_J,ActionEve
nt.ALT_MASK));

menuFileNewZip.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,ActionEve
nt.ALT_MASK));

menuFileSetRoot.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,ActionEv
ent.ALT_MASK));

menuFileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
ActionEvent.ALT_MASK));
menuFileExit.getAccessibleContext().setAccessibleDescription("Exit");

menuFile.add(menuFileNewJar);
menuFile.add(menuFileNewZip);
menuFile.add(menuFileOpenJar);
menuFile.add(menuFileOpenZip);
menuFile.add(menuFileSetRoot);
menuFile.add(menuFileSignJar);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
menuItem.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e)
{
//-- Create a JarFileFrame --
JarFileFrame JIF = new JarFileFrame("pickles");
desktop.add(JIF);
try
{
JIF.setSelected(true);
}
catch (java.beans.PropertyVetoException ee)
{
}
//---------------------
}
});

menuFileNewZip.addActionListener
(
new ActionListener()
{

public void actionPerformed(ActionEvent e)
{
ZipFileFilter myfilter=new ZipFileFilter();
JFileChooser fc = new JFileChooser();
fc.resetChoosableFileFilters();
fc.setFileFilter(myfilter);

fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int lnReturnVal = fc.showSaveDialog(w);

ZipFileFrame JIF = new
ZipFileFrame(fc.getSelectedFile().getAbsolutePath(),desktop);
File loJarOutFile = fc.getSelectedFile();

if (loJarOutFile.exists())
{
String title = "New";
JOptionPane loOverwriteConfirm = new JOptionPane();
int lnResult=loOverwriteConfirm.showConfirmDialog(w,"File
exists, overwrite?","File Exists",JOptionPane.YES_NO_OPTION);
if(lnResult==0)
{
desktop.add(JIF);
try
{
JIF.setSelected(true);
}
catch (java.beans.PropertyVetoException ee)
{
}
}
}
else
{
desktop.add(JIF);
try
{

Zip outzip = new
Zip("*.*",loJarOutFile.getCanonicalPath(),"c:\\Applets");
}
catch (java.io.IOException eee)
{
}

try
{
JIF.setSelected(true);
}
catch (java.beans.PropertyVetoException ee)
{
}
}



}
}
);

menuFileOpenZip.addActionListener
(
new ActionListener()
{

public void actionPerformed(ActionEvent e)
{

JarFileFilter myfilter=new JarFileFilter();
JFileChooser fc = new JFileChooser();
fc.resetChoosableFileFilters();
fc.setFileFilter(myfilter);

fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int lnReturnVal = fc.showOpenDialog(w);

ZipFileFrame JIF = new
ZipFileFrame(fc.getSelectedFile().getAbsolutePath(),desktop);
File loJarOutFile = fc.getSelectedFile();


desktop.add(JIF);
try
{


JIF.openZipFile(loJarOutFile.getCanonicalPath());
}
catch (java.io.IOException eee)
{
}

try
{
JIF.setSelected(true);
}
catch (java.beans.PropertyVetoException ee)
{
}




}
}
);

menuFileSignJar.addActionListener
(
new ActionListener()
{

public void actionPerformed(ActionEvent e)
{

if (JarSignerPath==null)
{
JarSignerFilter myfilter=new JarSignerFilter();
JFileChooser fc = new JFileChooser();
fc.resetChoosableFileFilters();
fc.setFileFilter(myfilter);

fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fc.setDialogTitle("Where is JarSigner?");
int lnReturnVal = fc.showOpenDialog(w);

File loJarSignerFile = fc.getSelectedFile();
JarSignerPath=loJarSignerFile.getAbsolutePath();

}
JarFileFilter myfilter=new JarFileFilter();
JFileChooser fc = new JFileChooser();
fc.resetChoosableFileFilters();
fc.setFileFilter(myfilter);

fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fc.setDialogTitle("Jar File to Sign?");
int lnReturnVal = fc.showOpenDialog(w);

File loJarOutFile = fc.getSelectedFile();
exec(JarSignerPath);


}
}
);


w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.setSize(700,500);
w.setLocation(50,50);
w.setVisible(true);
}






}
 
A

Andrew Thompson

Darren said:

Sentences that are easy to read are more likely to get
read. Using an uppercase character at the beginning
of a sentence helps to make posts easier to read.
...invoking to use Java's 'exec' but the documentation says its in the
java.lang.runtime class..

That is becasue it is.
..i import it

No need. java.lang.* is imported by default.
..as well as java.lang.process
ditto.

.. but the
when i compile the programme that calls it i get a "cannot resolve symbol"
on the "exec" line. which is 'exec(JarSignerPath);'

Please always copy/paste errors, or the first lines
of stack traces.
The code for this class alone is below;

252 lines of uncompilable* code is unlikely to get much
attention. If you need to post code, please post either
a very short snippet, or an SSCCE.
<http://www.physci.org/codes/sscce.jsp>

* had to fix code lines broken by being too wide, and
hunt through the errors caused by the missing classes.

But to your more immediate problem..
File loJarOutFile = fc.getSelectedFile();
exec(JarSignerPath);

F:\...Jarmaker\Jarmaker.java:227: cannot find symbol
symbol: method exec(java.lang.String)
exec(JarSignerPath);
^

<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#exec(java.lang.String)>
OK. 'exec(String)' is a method of a Runtime object. if we had
a runtime object called 'runtime' you might invoke it using..

runtime.exec("the command");

If that method were declared 'static', you would not even
require the object, but instead invoke it from the static context..

Runtime.exec("the command");

...in your code, your call effectively becomes..

this.exec("the command");

...where 'this' is an instance of Jarmaker, which has no 'exec'
method.

The error is saying there is no 'exec(String)' method for
the Jarmaker class, because that is where the compiler
thinks you mean to find it.

To get a instance of a Runtime object, see
<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#getRuntime()>

And, as 2 asides..
1) It pays to ask a question - though we've already been there. ;-)
2) The best groups for helping those new to Java is..
<http://www.physci.org/codes/javafaq.jsp#cljh>

HTH
 
B

Bjorn Abelli

Darren said:
i'm invoking to use Java's 'exec' but the documentation says its in the
java.lang.runtime class.. i import it as well as java.lang.process but the
when i compile the programme that calls it i get a "cannot resolve symbol"
on the "exec" line. which is 'exec(JarSignerPath);'

Due to the strictness of OO in Java, any method must be an instance method
or a class method.

This also affects how you can call it, i.e. *only* through an instance or a
class. When you don't provide a "receiver" for the call, the current
instance or class is considered the default reciever.

To take the "exec"-method as an example.

You don't have an "exec"-method in your class JarMaker, because exec is an
instance method for a Runtime-object.

Hence you can't call it just by:

exec(whatever);

....you have to have an instance to send the message exec to, e.g.

myRuntime.exec(whatever);

There's also a class method to retrieve a reference to the "current"
Runtime-instance, so here's how you can proceed:

Runtime myRuntime = Runtime.getRuntime();
myRuntime.exec(JarSignerPath);

I can't guarantee that it will work in your particular case, as I didn't
check if your "JarSignerPath" is valid as an argument to exec.

To read further:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html

// Bjorn A
 
K

klynn47

Remember that Java is case sensitve. It isn't java.lang.runtime. It's
java.lang.Runtime
 
D

Darren

Andrew Thompson said:
Sentences that are easy to read are more likely to get
read. Using an uppercase character at the beginning
of a sentence helps to make posts easier to read.

You can read, 'I'm' but you can't read 'i'm'?
That is becasue it is.


No need. java.lang.* is imported by default.


ditto.

Yo see I didn't know that and even then I don't take things as red because
if I do then knowing my luck a future version will not include them as red.
:\
Please always copy/paste errors, or the first lines
of stack traces.

I did,
"cannot resolve symbol"
252 lines of uncompilable* code is unlikely to get much
attention. If you need to post code, please post either
a very short snippet, or an SSCCE.
<http://www.physci.org/codes/sscce.jsp>

Look I'm not being funny but will you guys make up your minds? I got moaned
at for not posting the whole code now i did you moan at me. Why don't you
guys decide what you want and put it in the faq then we all know where we
are. or perhaps you have already and I missed it :)
* had to fix code lines broken by being too wide, and
hunt through the errors caused by the missing classes.

But to your more immediate problem..


F:\...Jarmaker\Jarmaker.java:227: cannot find symbol
symbol: method exec(java.lang.String)
exec(JarSignerPath);
^

<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#exec(java.la
ng.String)>
OK. 'exec(String)' is a method of a Runtime object. if we had
a runtime object called 'runtime' you might invoke it using..

runtime.exec("the command");

If that method were declared 'static', you would not even
require the object, but instead invoke it from the static context..

Runtime.exec("the command");

..in your code, your call effectively becomes..

this.exec("the command");

..where 'this' is an instance of Jarmaker, which has no 'exec'
method.

hehe well i thought it would include it as i did the import
The error is saying there is no 'exec(String)' method for
the Jarmaker class, because that is where the compiler
thinks you mean to find it.

To get a instance of a Runtime object, see
<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#getRuntime()

Many thanks. I replaced it with 'Runtime.getRuntime().exec(JarSignerPath);'

I know i could have done it tidier but it works.
And, as 2 asides..
1) It pays to ask a question - though we've already been there. ;-)

Yeah i haven't read your reply to tthat yet :)
2) The best groups for helping those new to Java is..
<http://www.physci.org/codes/javafaq.jsp#cljh>
Hey we should all help each other and my proggy isn't bad for one written by
a newbie :)

It did. thanks a lot
 
D

Darren

Bjorn Abelli said:
Due to the strictness of OO in Java, any method must be an instance method
or a class method.

This also affects how you can call it, i.e. *only* through an instance or a
class. When you don't provide a "receiver" for the call, the current
instance or class is considered the default reciever.

To take the "exec"-method as an example.

You don't have an "exec"-method in your class JarMaker, because exec is an
instance method for a Runtime-object.

Hence you can't call it just by:

exec(whatever);

...you have to have an instance to send the message exec to, e.g.

myRuntime.exec(whatever);

There's also a class method to retrieve a reference to the "current"
Runtime-instance, so here's how you can proceed:

Runtime myRuntime = Runtime.getRuntime();
myRuntime.exec(JarSignerPath);

I can't guarantee that it will work in your particular case, as I didn't
check if your "JarSignerPath" is valid as an argument to exec.

To read further:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html

// Bjorn A
Very helpful, thanks very much.
 
A

Andrew Thompson

Darren said:
Look I'm not being funny but will you guys make up your minds?

I am but one person, I have my opinions.

Some people agree with me, some people don't, others don't care
enough about what I have to say to listen long enough to make a
choice.
..I got moaned
at for not posting the whole code now i did you moan at me.

Who asked you for the whole code? I requested an *SSCCE*,
that is a very different thing, with some very specific
requirements. Requirements not met by your code.

Consider that there were fixes required to see the code run.
If two people wanted to see it, both of them had to fix the
errors, errors you might not have posted in the first place,
had you prepared an SSCCE.
.. Why don't you
guys decide what you want and put it in the faq

...Is this a troll, Darren? [1]

The closest thing that this group has to an FAQ is mentioned
in the Java mini-FAQ posted regularly to this, and several other
groups. The Java FAQ that is mentioned, resides at my site, and
was compiled by me, with the help of a number of other people.

Having said that, the words I put in that FAQ are not entirely
acceptable to all participants of these groups, and it is not
the 'official' FAQ of this (or any other) Java group.

[1] If so - "Ya' Got Me!" ;-)
Hey we should all help each other and my proggy isn't bad for one written by
a newbie :)

I do not understand what you are getting at, but I will be more plain.

I would not normally offer the level of description and detail
I did in that post, at least, not here on c.l.j.programmer.

OTOH, I (as well as some other people who help out on c.l.j.h.)
set out to 'take more time' when answering questions on c.l.j.help.
Also, the general nature of the discussion on help is different,
there are things (accusations, derision..) flung about on
c.l.j.programmer without comment, that would prompt a severe
criticism on c.l.j.help.
It did. thanks a lot

You're welcome. :)
 
M

Michael Dunn

Darren said:
i'm invoking to use Java's 'exec' but the documentation says its in the
java.lang.runtime class.. i import it as well as java.lang.process but the
when i compile the programme that calls it i get a "cannot resolve symbol"
on the "exec" line. which is 'exec(JarSignerPath);'

The code for this class alone is below;
<snipped>

I don't have some of the classes required to run the program, but if
everything
unrelated to the problem is stripped, and hard-code a string representing a
program
to run (which seems to be the intention of 'exec(..)') - in this e.g. using
notepad,
the compiler generates a "can't find symbol" error (after leaving all the
imports as is)

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionEvent;

import java.io.File;
import java.lang.Runtime;
import java.lang.Process;
class Jarmaker
{
private static String JarSignerPath="notepad.exe";
public static void main(String[] args)
{
exec(JarSignerPath);
}
}

exec is unknown because it is a method of Runtime
Runtime may be imported, but it is not used

change
exec(JarSignerPath);
to
try{Runtime.getRuntime().exec(JarSignerPath);}catch(Exception e){}

compile/run and see what happens
 
D

Darren

Andrew Thompson said:
I am but one person, I have my opinions.

Some people agree with me, some people don't, others don't care
enough about what I have to say to listen long enough to make a
choice.


Who asked you for the whole code? I requested an *SSCCE*,
that is a very different thing, with some very specific
requirements. Requirements not met by your code.

I had to look up *SSCCE*
Consider that there were fixes required to see the code run.
If two people wanted to see it, both of them had to fix the
errors, errors you might not have posted in the first place,
had you prepared an SSCCE.

No problem, but it's misunderstanding like this that call for a group FAQ
and no that wasn't a troll.
.. Why don't you
guys decide what you want and put it in the faq

..Is this a troll, Darren? [1]

Nope. :)
The closest thing that this group has to an FAQ is mentioned
in the Java mini-FAQ posted regularly to this, and several other
groups. The Java FAQ that is mentioned, resides at my site, and
was compiled by me, with the help of a number of other people.

Perhaps the group might see it as a suitable one for this group.
Having said that, the words I put in that FAQ are not entirely
acceptable to all participants of these groups, and it is not
the 'official' FAQ of this (or any other) Java group.

[1] If so - "Ya' Got Me!" ;-)

LOL no, you still win, it wasn't a troll. :)
I do not understand what you are getting at, but I will be more plain.

I would not normally offer the level of description and detail
I did in that post, at least, not here on c.l.j.programmer.

It was very kind of you to do so.
 
D

Darren

Michael Dunn said:
Darren said:
i'm invoking to use Java's 'exec' but the documentation says its in the
java.lang.runtime class.. i import it as well as java.lang.process but the
when i compile the programme that calls it i get a "cannot resolve symbol"
on the "exec" line. which is 'exec(JarSignerPath);'

The code for this class alone is below;
<snipped>

I don't have some of the classes required to run the program, but if
everything
unrelated to the problem is stripped, and hard-code a string representing a
program
to run (which seems to be the intention of 'exec(..)') - in this e.g. using
notepad,
the compiler generates a "can't find symbol" error (after leaving all the
imports as is)

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionEvent;

import java.io.File;
import java.lang.Runtime;
import java.lang.Process;
class Jarmaker
{
private static String JarSignerPath="notepad.exe";
public static void main(String[] args)
{
exec(JarSignerPath);
}
}

exec is unknown because it is a method of Runtime
Runtime may be imported, but it is not used

change
exec(JarSignerPath);
to
try{Runtime.getRuntime().exec(JarSignerPath);}catch(Exception e){}

compile/run and see what happens
did that and it worked.
Thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top